Use replace to remove all DOM which included id in javascript?

57 views Asked by At

I need to export .xls but there are some elements I don't need in the table. Can I use replace to remove all DOM which included "id" elements?

Ttable

<td>
<input id="id-1" value="111"><br>
<p">111</p>
<input id="id-2" value="222"><br>
<p">222</p>
<input id="id-3" value="3"><br>
<p">333</p>
<input id="id-4" value="4"><br>
<p">444</p>
<input id="id-5" value="4"><br>
<p">555</p>
</td>

I want to export like:

<td>
<br>
<p">111</p>
<br>
<p">222</p>
<br>
<p">333</p>
<br>
<p">444</p>
<br>
<p">555</p>
</td>

I tried to use replace to search all id^* element, but I can't remove the DOM. I want to remove all DOM which included "id" elements in xls.

1

There are 1 answers

0
Afzal K. On

picks up all the elements in the document that have an id attribute starting with the letters id. Then, it loops through each of these selected elements and removes them from the document.

const elementsToRemove = document.querySelectorAll('[id^="id"]');

elementsToRemove.forEach(element => element.remove());
<td>
<input id="id-1" value="111"><br>
<p>111</p>
<input id="id-2" value="222"><br>
<p>222</p>
<input id="id-3" value="3"><br>
<p>333</p>
<input id="id-4" value="4"><br>
<p>444</p>
<input id="id-5" value="4"><br>
<p>555</p>
</td>