Skip to content
Advertisement

JS, How to change only parent element

I have something like that :

<h3 id="plop">I want to change this text <span id="trololo">bla bla bla</span> </h3>

Generaly, I use js to change text on my html elements :

document.getElementById("plop").innerHTML = "I change the text";

But do something like this remove my span element on title. Then, is it possible to change only the text on h3 ?

I know, i can do something like that:

document.getElementById("plop").innerHTML = "I change the text <span id="trololo">bla bla bla</span>";

But is not my goal.

Advertisement

Answer

You can try this:

document.querySelector('#plop').firstChild.nodeValue = "I change the text "
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement