I need to modify the following HTML using Javascript,
<div id="1">
some text
<div id="2"></div>
</div>
I have tried using $('#1').text('new text');
However, this unintentionally removes <div id="2">
How should I change some text
, without changing the surrounding elements?
This will change the value of the first node (which is a text node in your example):
$('#1').contents()[0].nodeValue = 'new text';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With