Skip to content Skip to sidebar Skip to footer

I Need The Solution To A Javascript DOM Textnode Selection Using Javascript?

How would I get all the text nodes in this using javascript? I've tried so many things but them seam not to be working? )); for(var i= 0, L= nodes.length; i<L; i++){ // do whatever you want to the node var pa= nodes[i].parentNode; if(pa.nodeName== 'H4') pa.style.color= 'blue'; text.push(nodes[i].data); } alert(text.join(' '))

Solution 2:

If you just want the text, you can do:

var el = document.getElementById('product-1');
alert(el.textContent || el.innerText);

The above can be turned into a general "getText" function fairly easily.

If you want to get the actual text nodes, you have to look at each DOM element and get its child nodes, then iterate over each child node looking at whether it's an element (node type 1) or text (node type 3).


Post a Comment for "I Need The Solution To A Javascript DOM Textnode Selection Using Javascript?"