Jump to content

"For" loop to check for number of DIVs on a page.


ShootingBlanks

Recommended Posts

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!!!

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.