0
Why is this code not working
Im trying to get the first child node of the div element and change its contents but my code is not working. Please have a look and tell me my mistake https://code.sololearn.com/Wu0dTrFNXnpA/?ref=app
8 Answers
+ 4
What do you mean by printing it's "content"?
+ 4
Make that children from childNodes.
children creates only elements list but childNodes
creates a list of textNode and elements.
+ 3
childNodes is used to access all nodes, while children filter only elements (tags) nodes...
the last is useful for only walking tags, while the first is useful if you need to access any other nodes (text nodes, comments...)
the last is commonly enough and even preferable (think of a document where tags are indented against minified/inlined version without spaces between tags: the first has text nodes between tags, but not the last: children equals childNodes for the last, but not the first)
+ 1
Abhay thanks it worked after I made the change. If the childNodes method creates a list of elements and text nodes what is it used for?
+ 1
Uchiha sorry! ,but I don't know.
0
Ezra Bridger 2207 sorry I meant 'change its content' thanks for letting me know
0
Abhay No problem, thank you for all the help
0
visph Thank you