dsjoes Posted March 11, 2011 Share Posted March 11, 2011 is there any way to loop through list elements using JavaScript moving one element at a time on button click example list <ul><li>Message 1</li><li>Message 2</li><li>Message 3</li></ul> Quote Link to comment https://forums.phpfreaks.com/topic/230317-loop-through-list-elements/ Share on other sites More sharing options...
nogray Posted March 11, 2011 Share Posted March 11, 2011 you can use document.getElementsByTagName('tag name here') to get an array of the elements. e.g. HTML <ul id="my_list"><li>message 1</li><li>message 2</li></ul> Script var ul = document.getElementById('my_list'); var li_arr = ul.getElementsByTagName('li'); for (var i=0; i<li_arr.length; i++){ // do something } If you are going to remove or add elements from the parent, you would need to use a while loop instead of for loop since the length property will change. Quote Link to comment https://forums.phpfreaks.com/topic/230317-loop-through-list-elements/#findComment-1186423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.