Search code examples
jqueryhtmlinnerhtml

Jquery: change html knowing node inside


I've this code:

<p> &nbsp &nbsp <span> text </span> &nbsp &nbsp </p>

how to delete second and third &nbsp of p, through the node span?


Solution

  • Get child nodes of p and remove space from text nodes

    var p = document.getElementsByTagName('p')[0];
    
    var nodes = p.childNodes;
    
    nodes[0].textContent = nodes[0].textContent.replace(' ', '');
    nodes[2].textContent = nodes[2].textContent.replace(' ', '');
    <p>&nbsp &nbsp <span> text </span> &nbsp &nbsp</p>