how to parse a string to HTML with jquery

My string looks like this:

<table>
 <tbody>
   <tr>
     <td>Sack</td>
     <td>&nbsp;</td>
     <td>7.48</td>
     <td>&nbsp;</td> 
    </tr>

    <tr>
      <td>Palette</td>
      <td>&nbsp;</td>
      <td>23.41</td>
      <td>&nbsp;?</td>
    </tr>
</tbody>

I would like to parse it with jquery, so I can add a <td> inside of the <tr>'s at the end.

I tried the following so far: Consider that "item[index].preis" containing the String without id or class.

var el = $.parseHTML(item[index].preis);
    console.log(el[1]);

[![enter image description here][1]][1]

I tried to access to the tr with .children() but it didnt work.

Answers 1

  • If you want to insert the same values at the end of each Table Row you can use the following:

    var rows = document.getElementsByTagName("tr");
    
    for (var i = 0; i < rows.length; i++)
    {
      var rowToInsert = rows[i];
      var insertPoint = rowToInsert.insertCell(-1);
      insertPoint.innerHTML = "New Value";
    }
    <table>
      <tbody>
        <tr>
          <td>Sack</td>
          <td>&nbsp;</td>
          <td>7.48</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>Palette</td>
          <td>&nbsp;</td>
          <td>23.41</td>
          <td>&nbsp;?</td>
        </tr>
      </tbody>
    </table>


Related Articles