Simulator88
Simulator88

Reputation: 637

Show Div and hide others with A Href onclick

I have a menu with some links. Like this:

<ul id="nav"><li><a href="#" class="tartalom">Some Div 1</a></li><li><a href="#" class="tartalom2">Some Div 2</a></li></ul> 

Here are my divs.

  <div class="tartalom1>Bla bla bla</div> 

  <div class="tartalom2>Yeeah yeah yeah</div> 

How to make it possible, if I click on the links to open that div with the same class and close the others if they are open? I tried this, but it wont help:

   $(document).ready( function(){
  $('.tartalom').click( function(){ // set of divs to be clickable
   $(this).siblings('div').hide(); // it's already showing, right?
});
});

Upvotes: 1

Views: 6975

Answers (1)

Adil
Adil

Reputation: 148110

You need to reach the enclosing div first to access siblings divs. You can use closest() function to find the nearest ancestor matching the search criteria.

Live Demo

$(document).ready( function(){
  $('.tartalom').click( function(){ // set of divs to be clickable
      $(this).closest('div').siblings('div').hide(); // it's already showing, right?
  });
});

You also missed the closing quotes of tartalom1 and tartalo2 class in divs. 

Upvotes: 2

Related Questions