<ul id='myList'>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
<script>
alert(document.getElementB
</script>
Main Topics
Browse All TopicsHi All.
I need to write a script that will walk through a few ul's.
Say for example I have the following HTML
<ul id='myList'>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
and I want to access the data from each li item through javascript.
[ eventually I'd like a JS function to cycle through the list and alter the contents using innerHTML or such]
my only issue is accessing the li items, i'd much rather not to have individual id's and such, but rather use DOM.
I have used the following code:
document.getElementById('m
this returns "" as in not null or undefined but just a empty string...
Im sure there has to be a way of doing this... any ideas?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I have never shyed away from a challenge, here is the recursion (the var i in the loop is crucial)
<script>
function printList(list){
for (var i=0;i<list.childNodes.leng
if (list.childNodes[i].childN
else alert(list.childNodes[i].i
return
}
</script>
<ul id="myList">
<li>a1</li>
<li><ul id="myList">
<li>a2</li>
<li>b2</li>
</ul>
</li>
<li>c1</li>
<li><ul id="myList">
<li>c2</li>
<li>d2</li>
<li><ul id="myList">
<li>a3</li>
<li>b3</li>
</ul>
</li>
</ul>
</li>
<li>d1</li>
<li>e1</li>
</ul>
<script>
printList(document.getElem
</script>
Business Accounts
Answer for Membership
by: d2kagwPosted on 2005-06-22 at 21:20:22ID: 14281433
I was also thinking of using getElementsByTag, but you can't limit its depths...
[ i'd eventually like to have a list like this ]
<ul id='myList'>
<li>a</li>
<li>
<ul id='myList'>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
</li>
<li>c</li>
<li>d</li>
<li>e</li>
</ul>
Cheers