I have an element that might be <p>
or <div>
or any other element. I want to get all textnodes of all the element with it's respective text .
var nav = document.getElementById('mynav');
function all(el){
var io = el.getElementsByTagName('*');
for (var i = 0; i < io.length; i++) {
var ex = io[i];
if (ex.firstChild.data.trim() !== '') {
console.log(ex.firstChild)
}
}
}
all(nav);
<div id="mynav">
text1
<div style="border:1px solid red; height:100px;">
text2
<div style="border:1px solid green; height:80px;">
text3
<div style="border:1px solid orange; height:60px;">
<div style="border:1px solid blue; height:40px;">
text5
</div>
text4
</div>
</div>
</div>
</div>
What my code does?
My above program gives three textnodes containing text( text2 , text3 and text5).
What I am trying to do?
I want to get all the textnodes containing all text i.e(text1 , text2 , text3, text4 and text5) even though my text(i.e: text4) is at the back of div contaning text5.
I think it is possible by using array of childNodes of an elements but not sure.
How can we solve this problem please help me.
Please don't use jquery.