1

Problem in short: I'm trying to make a simple show/hide toggle work on just the element you click on, while right now all (13) divs get affected by the function.

Structure of each div looks like this

<div class="selectbox">
    <h5>Title</h5>
    <p>
        Content
    </p>
</div>

With this jquery code

$('.selectbox h5').click(function(){
         $('div.selectbox,p,span').toggle();
         $('.selectbox').toggle();
    });

So right now, the jquery works. But obviously, does its work on every "selectbox" div on the page. How can I make this work on just the element I click on, without using a unique id for every "selectbox" div?

4 Answers 4

4
$('.selectbox h5').click(function(){
     $(this).parent().find('p,span').toggle(); 
     $(this).parent().toggle(); // this line should probably be removed 
});
Sign up to request clarification or add additional context in comments.

4 Comments

the 'p,span' line isn't useless ;) Works perfectly as you posted it, but not without that line
I was wondering. The last line is probably not desired in that case (if you'd want to toggle the thing back).
And you might be interested by this also.
Pretty much yes, with multiple checkboxes being the content.
3

use $(this) -

$('.selectbox h5').click(function(){

  $(this).closest('.selectbox, p, span').toggle();
  //$('.selectbox').toggle();

});

1 Comment

We posted the same solution at the same time. I can't resist upvoting your answer :)
2

You may use the keyword this in your function, it represents the item that triggered the event. In this case your H5.

$('.selectbox h5').click(function(){
         $(this).closest('.selectbox').toggle();
    });

Comments

1

Make it simple..

if you want the parent DIV to toggle then just use $(this).parent().toggle(); in your function. p.e.: If you want to work with the <p> in your div you may address it like $('p', $(this).parent()).toggle();

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.