Luc
Luc

Reputation: 843

How do you change an element innerText without changing its children

I have an html element like:

<div id="el1">Change only me<div>but not me</div></div>

but I only wanna change the first text and leave the child div as it is

document.getElementById("el1").innerText = "changed!"
<div id="el1">Change only me<div>but not me</div></div>

Upvotes: 4

Views: 6461

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28424

Try this:

document.getElementById("el1").childNodes[0].textContent = "changed";
<div id="el1">Change only me<div>but not me</div></div>

Upvotes: 13

Related Questions