Hoveltine Hoveltine - 6 months ago 8
Javascript Question

Want a specific value from webpage

<div class = "foo">
<span class = "foo2" > HeaderText</span>
<br>
"
TEXT I WANT"




I have been trying to get it in JS.

var NEEDED= document.getElementsByClassName('foo')[0].textContent


Also tried foo2 and innerText, innerHTML

I just don't know how to call something that isn't tied directly to an ID or Class

Answer

What you are trying to do is going to be very difficult if the HTML is subject to change. Even nodes that aren't surrounded by an HTML element are still childNodes of it's parent, so you can get this by looking at:

var needed = document.getElementsByClassName('foo')[0].childNodes[4].nodeValue;

You might need to write a function that filters through all of the childNodes and finds one with nodeType 3 (text nodes), then ignore all of the ones that are just blank whitespace.

var needed = document.getElementsByClassName('foo')[0].childNodes[4].nodeValue;
document.write("The text is: " + needed);
<div class = "foo">
   <span class = "foo2" > HeaderText</span>
   <br>
   TEXT I WANT
</div>

Comments