Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change innerHTML of childNodes in case some childnodes without tags?

That is my example of problem

<div onclick="this.childNodes(0).innerHTML='0';">
1<b>2</b>3<b>4</b>5
</div>

as you see, two childnodes ("2" and "4") are tagged, others are simple text. The question is how to change innerHTML of tagged and untagged nodes (texts) in sertain div container without touching other nodes/texts?

like image 847
el Dude Avatar asked Feb 18 '23 22:02

el Dude


1 Answers

Essentially, you'll use the data(text) property for text nodes (nodeType 3) and innerHTML otherwise (fiddle):

<div onclick="this.childNodes[0][this.childNodes[0].nodeType === 3 ? 'data' : 'innerHTML'] = '0'">
1<b>2</b>3<b>4</b>5
</div>​

[edit] I'm getting really tired of everyone offering libraries as solutions when all that's required is a simple explanation of a basic concept, e.g.: text-nodes and element nodes have differing content properties, i.e.: data and innerHTML.

like image 152
canon Avatar answered Feb 21 '23 10:02

canon