Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Add Sequential ID # To Div

    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.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    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.
    Code:
    var mydivs = document.getElementsByClassName("divclass");
    for (x=0;x<mydivs.length;x++)
    {
    mydivs[x].setAttribute{"id","widget"+x);
    }
    Javascript likes to start a 0 when numbering things.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Is this something I can use a PHP array for?

  • #4
    The Apostate Apostropartheid's Avatar
    Join Date
    Oct 2007
    Posts
    3,215
    Thanks
    16
    Thanked 265 Times in 263 Posts
    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? foreach is the usual function for array iteration.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •