I have some lists set up like the following:
<ul id="menu">
<li>
<a href="#">link a</a>
<ul>
<li>...</li>
<li>...</li>
</ul>
</li>
<li>
<a href="#">link b</a>
<ul>
<li>...</li>
<li>...</li>
</ul>
</li>
<li>
<a href="#">link c</a>
<ul>
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
I'm trying to pull the text from each of the first anchors (link a, link b, link c), but am having some problems.
Most recently, I've tried:
var X = document.getElementById("menu");
var Y = X.getElementsByTagName("li");
for (var i = 0; i < Y.length; i++) {
var Z = Y[i].getElementsByTagName('A');
console.log(Z[0].innerHTML);
}
but this jumps into the <ul>
s within the <li>
s.
What I need really is to be able to get the <a>
reference to the top level <li>
s and also be able to grab the text within the <a>
of those <li>
s.