-2
function addthis(){
        var title=document.getElementById("title").value;
        var desc=document.getElementById("desc").value;
        var newRow=document.querySelector('tbody');
        var newbody=`<tr>
                        <td>${title}<td>
                        <td>${desc}</td>
                    </tr>`;
        newRow.append(newbody);
    }

I was trying to add new row and inside that this content when user click on add to list but the output is its showing every content as <tr><td>hello<td><td>title</td></tr> like this.

what should be the right way to use this

Spectric
  • 30,714
  • 6
  • 20
  • 43

1 Answers1

-1

append will insert DOMString objects as Text nodes, escaping the HTML.

Use insertAdjacentHTML instead:

function addthis() {
  var title = document.getElementById("title").value;
  var desc = document.getElementById("desc").value;
  var newRow = document.querySelector('tbody');
  var newbody = `<tr>
                        <td>${title}<td>
                        <td>${desc}</td>
                    </tr>`;
  newRow.insertAdjacentHTML('beforeend', newbody);
}

addthis()
<input id="title" value="title">
<input id="desc" value="description">
<table>
  <tbody></tbody>
</table>
Spectric
  • 30,714
  • 6
  • 20
  • 43