ShootingBlanks Posted May 14, 2010 Share Posted May 14, 2010 Hello. I'd like to know how to cycle through a "for" loop based on the number of divs on a page there are that start with "SelectDiv". So if I have this: <div id="SelectDiv1"></div> <div id="SelectDiv2"></div> <div id="SelectDiv3"></div> <div id="SelectDiv4"></div> Then I want to know how to write a "for" loop that will do something four times. If one of those divs were removed, I'd need the "for" loop to do something three times. The first div will always be "SelectDiv1", and they will always go in numerical succession. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/201746-for-loop-to-check-for-number-of-divs-on-a-page/ Share on other sites More sharing options...
Psycho Posted May 14, 2010 Share Posted May 14, 2010 A while loop is a better solution IMHO. var divID = 1; while(document.getElementById(divID)) { var divObj = document.getElementById(divID); //Do something divID++; } Quote Link to comment https://forums.phpfreaks.com/topic/201746-for-loop-to-check-for-number-of-divs-on-a-page/#findComment-1058276 Share on other sites More sharing options...
ShootingBlanks Posted May 14, 2010 Author Share Posted May 14, 2010 A while loop is a better solution IMHO. var divID = 1; while(document.getElementById(divID)) { var divObj = document.getElementById(divID); //Do something divID++; } I'm kind of a novice at JS, so I'm not understanding some of your code. Where does the "divObj" var come into play? And how do I distinguish those "SelectDiv" divs from the rest of the divs on the page (since the "SelectDiv" divs are the only ones I'm concerned about)? Like, I didn't see "SelectDiv" anywhere in your code. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/201746-for-loop-to-check-for-number-of-divs-on-a-page/#findComment-1058289 Share on other sites More sharing options...
Psycho Posted May 14, 2010 Share Posted May 14, 2010 Sorry, I left out the 'SelectDiv' from the code: var divID = 1; while(document.getElementById('SelectDiv'+divID)){ var divObj = document.getElementById('SelectDiv'+divID); //Do something divID++; } The divObj is not technically necessary, I just assumed that you would be doing something with each div in your loop. If you don't need to do anything with the divs then you can remove it. To explain the code a little bit, the while loop will continue as long as the condition is true. So, when "document.getElementById('SelectDiv'+divID)" is in the condition it will return True if the element exists. So, starting at 1, the loop will continue incrementing divID until there is no div with the ID of "'SelectDiv'+divID" Quote Link to comment https://forums.phpfreaks.com/topic/201746-for-loop-to-check-for-number-of-divs-on-a-page/#findComment-1058312 Share on other sites More sharing options...
ShootingBlanks Posted May 14, 2010 Author Share Posted May 14, 2010 To explain the code a little bit, the while loop will continue as long as the condition is true. So, when "document.getElementById('SelectDiv'+divID)" is in the condition it will return True if the element exists. So, starting at 1, the loop will continue incrementing divID until there is no div with the ID of "'SelectDiv'+divID" Makes perfect sense. Thanks for the explanation AND the code help! Quote Link to comment https://forums.phpfreaks.com/topic/201746-for-loop-to-check-for-number-of-divs-on-a-page/#findComment-1058317 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.