locked
Is this a string ? I cannot truncate it RRS feed

  • Question

  • User-775831949 posted

    I am having this js code around a js utility I copied from their demo to get the row text from a HTML table:

    modelChanged: function (rowIndex, columnIndex, oldValue, newValue, row) 
    {
    displayMessage(row.innerHTML)
    }; function displayMessage(text) { document.getElementById("message").innerHTML = text
    }

    This code is displaying a row content (1 row 5 columns) of table into  <div id="message">Message</div> successfully.
    Eg it is like this 28 USA Green Apple 3000

    Now I only need to get the value before the first space (ie 28) only.
    So I modify the code as below but it does not work. Please help...
    Maybe the text is not a string ? What can I do ? Thanks

    function displayMessage(text) {
    document.getElementById("message").innerHTML = text.Spilt(' ')[0]}

    Saturday, January 20, 2018 8:40 AM

Answers

  • User-474980206 posted

    you are correct, innerHTML returns the markup (html code), not the text.

       

    modelChanged: function (rowIndex, columnIndex, oldValue, newValue, row) {
        // get row tds
        var tds = row.getElementsByTagName('td');
    
        //convert array of td to array of td text
        var cols = [];
        for (var i=0; i < tds.length; ++i) {
          cols.push(tds[i].textContent); 
        } 
    
        // display text of first td
        displayMessage(cols[o]); 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 20, 2018 6:09 PM

All replies

  • User-775831949 posted

    I just tried below it returns a number exceed 600. Seems it does not simply contain text, or maybe with tag ?

    document.getElementById("message").innerHTML = text.length

    if I use

    displayMessage(row) instead of displayMessage(row.innerHTML), the displayed message becomes

    [object HTMLTableRowElement]

    What can I do to just get that 28 in first column ?

    Thanks

    Saturday, January 20, 2018 8:55 AM
  • User-474980206 posted

    you are correct, innerHTML returns the markup (html code), not the text.

       

    modelChanged: function (rowIndex, columnIndex, oldValue, newValue, row) {
        // get row tds
        var tds = row.getElementsByTagName('td');
    
        //convert array of td to array of td text
        var cols = [];
        for (var i=0; i < tds.length; ++i) {
          cols.push(tds[i].textContent); 
        } 
    
        // display text of first td
        displayMessage(cols[o]); 
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 20, 2018 6:09 PM
  • User-775831949 posted

    It works, thank you so much.

    Sunday, January 21, 2018 7:57 AM