3

My question is related to this one: jQuery same click event for multiple elements

However, when I do:

$('.class1, .class2').click(function() {
   some_function();
});

I always use the same function with the same object. What if I wanted to consolidate the following two statements for instance:

$(".class1").click(function () {
  $(".classer1").slideToggle("slow");
});

$(".class2").click(function () {
  $(".classer2").slideToggle("slow");
});

They reference a different object for each - can I even consolidate this?

Thanks, Oliver

1
  • You could, but there's probably a better way to do it depending on the html structure. Commented Mar 20, 2013 at 21:14

5 Answers 5

5

You could do this:

$(".class1, .class2").click(function () {
  $(".classer"+$(this).attr('class').substr(-1)).slideToggle("slow");
});

as long as the elements you're clicking on only have one class. This pulls the integer off the element being clicked on and appends it to .classer. So clicking on .class2 would trigger the toggle on .classer2.

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

1 Comment

Almost feels unfair to select ONE answer as they've all been great, but I like this idea! Thanks!
2

You might want to take a look at add(). Use it to add selectors to the selection stack. Use it like this:

$('.class1').add('.class2').click(function (e) {
    // Do whatever you want
})

2 Comments

How does this answer the question at all?
maybe if he used this he will reference the object he clicked?
2
<div class='common-class' data-id='1'></div>

$('.common-class').on('click', function() {
    if ($(this).data('id')) { 
        $(".classer" + $(this).data('id')).slideToggle("slow");
    }
});

Comments

2

I think the best way to do this is to add a data attribute to the reference number you are using for each of the classes so if you have something like

<div class="class1" data-ref-id="1"></div>

and

<div class="class2" data-ref-id="2"></div>

You could find your referenced toggle class like so:

$('.class1, .class2').click(function (e) {
    $('.' + NAME_OF_REFERENCED_TOGGLE_CLASS + this.data.refId).slideToggle('slow');
})

4 Comments

you should put that e in its place in the function()
But the OP wants to toggle other elements, not the ones clicked on. .class1 vs .classer1
Exactly j08691 - that's the part I'm struggling with - toggling MULTIPLE items. Or is this possible with target and I don't get it?
Ah sorry I misread what was inside. I think my updated answer should help.
1

It might be better to select based on the elements location depending on your html. For example, if this were your html:

<dl class="class1 list">
    <dt>Foo</dt>
    <dd>Foobar</dd>
    <dt>Dog</dt>
    <dd>Pitbull</dd>
</dl>
<dl class="class2 list">
    <dt>Foo</dt>
    <dd>Foobar</dd>
    <dt>Dog</dt>
    <dd>Pitbull</dd>
</dl>

you could use:

$(".list dt").click(function(){
    $(this).next("dd").slideToggle();
});

to handle both lists with a single click event.

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.