Skip to content
Advertisement

How do I access a list where “li” elements contain it’s child “a”?

var array = [];

<ul>
<li><a href="#"> First </a></li>
<li><a href="#"> Second </a></li>
<li><a href="#"> Third </a></li>
</ul>

I want to put every “a” element into array. After I access to a “li” element, I can’t go any further in reaching element “a”.

I know it looks easy, but I can’t figure it out for some reason, please help

Advertisement

Answer

It’s not very clear what your after here. But here is a simple example of how you can get these elements in a browser.

var array = document.querySelectorAll('li a');
console.log(array);
<ul>
<li><a href="#"> First </a></li>
<li><a href="#"> Second </a></li>
<li><a href="#"> Third </a></li>
</ul>

the query li a says select all a contained in a li tag. Docs on querySelectorAll

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement