-2

I'm probably doing something obviously wrong, but I just can't see it. I'm trying to use a for loop to define multiple click events and am experiencing an unexpected result. Some of this is working (the hide and show at the beginning of the function, but both sections wind up targeting the second item in the loop. Could somebody take a look at this and tell me what I'm doing wrong? Thank you so much for your help!

here is the link: http://grana.us/test/expand2.html

4
  • Post some code, so that we can help u. Commented Oct 26, 2013 at 12:00
  • Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. Commented Oct 26, 2013 at 12:06
  • @MikeW the problem is obvious in this case... Commented Oct 26, 2013 at 12:09
  • @FlashThunder That's not the point. If the OP removes the page at the link the question and the answers become meaningless. Stack Overflow is intended to become an archive of useful questions and answers that can be used in the future. Commented Oct 26, 2013 at 12:16

1 Answer 1

1

You are assigning same event to all summaries for every id. This is wrong...

First... to hide all details and show all toglers simply use:

$('.details').hide();
$('.toggler').show();

And then define click function to all sumaries:

$('.summary').click(function(){
   if($('.toggler',this).html() == ' -'){
      $('.toggler',this).html(' +');
      $('.details',$(this).parent()).hide();
   }else{
      $('.toggler',this).html(' -');
      $('.details',$(this).parent()).show();
   }
});

Put everything in...

$(function(){
   ...
});

and should be ok.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.