View Full Version : Add Sequential ID # To Div

07-27-2012, 04:40 PM
This should be pretty simple but I'm more of a CSS guy rather than a PHP guy. What is the callout to add a sequential number for every instance. I want all of my widget divs to be numbered.

For instance, I want the first displayed widget to be contained in <div id="widget1"> and the second to be contained in <div id="widget2">, etc. So the source code for widget content should be <div id="widget???"> but I don't know what to stick in the ??? to make it number things sequentially.

07-27-2012, 05:05 PM
You have to use javascript to find all the divs, you could assign them a class name and find them that way too, then setAttribute to give them an id.

var mydivs = document.getElementsByClassName("divclass");
for (x=0;x<mydivs.length;x++)
}Javascript likes to start a 0 when numbering things.

07-27-2012, 05:19 PM
Is this something I can use a PHP array for?

07-28-2012, 02:15 AM
Absolutely, yes, but why you posted in the HTML & CSS forum when you recognise it's purely a PHP problem is confusing.

Why don't you show what you tried and post again over there (http://www.codingforums.com/forumdisplay.php?f=6)? foreach is the usual function for array iteration.

It's also probably possible to do this with nth-child if you really wanted to.