downa1234 downa1234 - 6 months ago 8
Javascript Question

How to get all the li in javascript?

<ul id="tab">
<li onclick="clicker()" class="li01">one test</li>
<li onclick="clicker()" class="li02">two test</li>
<li onclick="clicker()" class="li03">three test</li>
<div class="web_clear"></div>
</ul>
<div class="web_index">
content....
</div>


I used
document.getElementsByTagName("ul").childNodes;
to get all the
li
. But it doesn't work.

Answer

You can use the id

document.getElemensById("tab").childNodes;

Or if its the only ul then

document.getElementsByTagName("ul")[0].childNodes;
Comments