show one div hide all another div with javascript

Discussion in 'JavaScript' started by dineshsingh1984, Jun 29, 2012.

  1. #1
    I want to a script where onclick show one div and hide another all div with java script.......

    here number of div 1 2 3 4 5 6......n

    plz tell me script........
     
    Last edited: Jun 29, 2012
    dineshsingh1984, Jun 29, 2012 IP
  2. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #2
    You can give your divs id like myDiv1, myDiv2, ..etc
    and in a for loop hide all the divs except the clicked one.
    You can use a code somwhat for hiding
    
    for (i = 1; i <= n; i++) {
    if (clickedDiv != document.getElementById('myDiv' + i)) {
    document.getElementById('myDiv' + i).style.display="none";
    document.getElementById('myDiv' + i).style.visibility="hidden";
    }
    }
    
    Code (markup):
    You should then call this method on div's onClick event and pass 'this' as parameter to the method.
     
    Unni krishnan, Jun 29, 2012 IP
  3. HydroJohn

    HydroJohn Active Member

    Messages:
    1,558
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    90
    #3
    HydroJohn, Jul 1, 2012 IP