Some very strange behaviour here:
What I am doing:
- dynamically setting class name to a
td
- dynamically appending a
div
to thetd
My then resulting td
ends up like this:
<td class="GSCBRI<div><p>Telefon : </p></div> 17"> <div> <p>Telefon : </p> </div> </td>
<td class="GSCBRI 17"> <div> <p>Telefon : </p> </div> </td>
Naturally my code:
row.childNodes.forEach((el) => {
el.setAttribute('class', `${row.childNodes[0].innerHTML} ${i}`);
i++;
let htmlDiv = document.createElement('div');
let htmlp = document.createElement('p');
htmlp.innerText = 'Telefon : ';
htmlDiv.appendChild(htmlp);
htmlDiv.setAttribute('style', 'display:none;')
el.appendChild(htmlDiv);
})
If I comment out the el.appendChild(htmlDiv) it does not interfere with my class, but then I of course do not get the div
How is my div
getting caught up in my class?
2
Answers
Based on T.J. Crowder's helpful observation I fixed the code like this:
Instead of using
.childNodes
, it will be easier with using.children
. It returns all of the children as HTML elements instead of the HTML nodes.