0

I have included some textual content in a div element. I want to access individual nodes out of it in the form of strings. Some nodes are tagged in bold, and therefore the rest of nodes that are formed in the text are non-tagged. I am trying to access all the individual nodes thus formed using nodeList, or through a simple indexed loop using childNode property. I have even used document.queryselectorAll(), but nothing seems to work.Following is the code snippet:

<!DOCTYPE html>
<html>
  <body>
  <div id="mydiv" class="myDiv" contenteditable="true">This <b>life</b> is my choice, <b>anyway</b> I know how to lead a good one!!!</div>
</body>
<script>
 let myString ='';
            for(let i=0; i<mydiv.childNodes.length;i++){
         myString = mydiv.childNodes[i]+mydiv.childNodes[i+1]+mydiv.childNodes[i+2]
       }
console.log(myString)
</script>
</html>

I'm getting this at the console: [object Text][object HTMLElement][object Text]

H Dhindsa
  • 31
  • 1
  • 6
  • What do you want to do with the nodes, as you seem to already get them? – Teemu Jul 02 '22 at 08:59
  • what exactly do you want? Is is not so clear. Please be specific and as clear as you can be. – DecPK Jul 02 '22 at 09:00
  • I want to get node outputs in the form of strings that can be manipulated , like being able to get appended as shown here. – H Dhindsa Jul 02 '22 at 09:04
  • Umm ... Nodes are not strings, as you can determine based on stringified format. You're already getting the nodes, just use them for what ever you need. Ex. `document.body.append(mydiv.childNodes[i])` or get the text of a node like `mydiv.childNodes[i].textContent` etc. Check [the properties of nodes](https://developer.mozilla.org/en-US/docs/Web/API/Node) at MDN. – Teemu Jul 05 '22 at 06:13

0 Answers0