close/hide some div's on load

Discussion in 'JavaScript' started by MC_delta_T, Apr 29, 2010.

  1. #1
    i have a scheme on my page like this

    <div id="abc_alt">....</div>
    <div id="def_alt">....</div>
    <div id="123_alt">....</div>
    <div id="ad56_alt">....</div> 
    Code (markup):
    i can close/open ( show/hide ) div from what i want


    hideLevel(levelId)
    Code (markup):
    on my page loaded, all divs are open. i want to close all divs at that moment. all divs has a same point as you can see. they ended with "_alt".

    how can i do that?

    sorry for my language.
     
    MC_delta_T, Apr 29, 2010 IP
  2. d_s

    d_s Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    You can use any numerical values for your divs like div0,div1 etc.. By this, you can easily get the count of the no. of divs and then use a for loop to generate the divid aslike
    
    for(var i=0;i<5;i++)
    {
         var divId = "div"+i;
         document.getElementById(divId).style.display = "none";
    }
    
    Code (markup):
    this would ease the job than having a common prefix. Your option is also good, if you use some sort of numbers, it will be easy to generate the ID's.

    you can give this a try and leave me a feedback. In case of any other javascript topics, do checkout my blog at
    http://www.dsaravanan.wordpress.com/javascript/

    regards

    d_s

     
    d_s, Apr 30, 2010 IP