SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2006
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making multiple divs appear onClick

    So as a nube at javascript I have perfected the art of making divs appear and disappear onClick or focus or whatever.

    I ran into a problem though last night when I went to make a link that would make multiple divs all with the same id appear. Well I am sure you guys already know, but it only affected the first div with the id and none of the rest.

    How do I get past this without having to name every div indiviually? The reason being this is will be used inside of a php while statment that produces the divs. So there could be 10 one time 5 the next or thrity the next.

    Thanks for the help.

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not encase them in a wrapper - then show/hide the wrapper?

  3. #3
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It it's in PHP, it should be fairly simply to give each DIV an individual ID, by adding an incrementing number to each. You can then either make a Javascript which loops through all the DIVs on the page, or simply generate the Javascript in PHP as well.

    It sounds like you are making something which should be a CLASS, though, not an ID.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  4. #4
    SitePoint Zealot bensheard's Avatar
    Join Date
    Jun 2006
    Location
    Cape Town | South Africa
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    The above things would work, but is there not a way to keep them with the same id? I have a similar case and it would make the code cleaner as well. Also, if you use a for loop, which is what I normally do use, you have to change the value of i (or whatever variable you use) everytime you add a new element that you want to be affected by this code. If you use getElementById and there are multiple elements with the same id, does it not create an array or something?

    Just wondering...
    Ben Sheard
    -------------
    Log all your dives easily and quickly from anywhere in the world
    www.divinglogs.cafe150.com

  5. #5
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It's impossible to use the same ID for all the DIVs. It isn't even allowed in HTML, so your HTML will be invalid as well. Each ID can only be placed once on each page.
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


Bookmarks

Posting Permissions

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