0
<div class="test">
 <button>Example</button>
 <div class="example" style="display:none;">Blah</div>
</div>

<div class="test">
 <button>Example</button>
 <div class="example" style="display:none;">Another</div>
</div>

When button gets clicked, I want .example to .show, but only the .example that's in the current .test.

3 Answers 3

3

Another way that works for your particular HTML:

$('button').click(function(){
    $(this).next('.example').show();
});
Sign up to request clarification or add additional context in comments.

Comments

1

This will do exactly what you said:

$('button').click(function(){
    $(this).closest('.test').find('.example').show();
});

This works too for the markup you posted, but it won't work if the button and .example are not siblings:

$('button').click(function(){
    $(this).siblings('.example').show();
});

Comments

0

Hiya Working demo for your case here: http://jsfiddle.net/4bLZF/

this uses slideToggle http://api.jquery.com/slideToggle/

code

  $(document).ready(function () {

    // Watch for clicks on the "slide" link.
    $('button').click(function () {
        $(this).next(".example").slideToggle(400);
        return false;
    });


});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.