Nicole Harris
Nicole Harris

Reputation: 860

Combining jquery selectors

I want to select #one, #two and the siblings of #two. Is there a more elegant solution that what I have?

     $('#one, #two').click(function(e){
         e.stopPropagation();
     });

     $('#two').siblings().click(function(e){
         e.stopPropagation();
     });

Upvotes: 2

Views: 302

Answers (4)

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

The multiple selector exists;

$("#one, #parentIdOfTwo > *").click(function(e) { 
    e.stopPropagation();
});

Lot faster, too.

Upvotes: 2

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this. Take a look I am using andSelf which add the previous set of elements on the stack to the current set, really helpful in your case here.

$('#one').add($('#two').siblings().andSelf()).click(function(e){
      e.stopPropagation();
});

Upvotes: 1

Mathias Bynens
Mathias Bynens

Reputation: 149534

You could use:

$('#one, #two').add($('#two').siblings()).click(function(event) {
  event.stopPropagation();
});

…or:

$('#one').add($('#two').parent().children()).click(function(event) {
  event.stopPropagation();
});

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148524

 $('#one, #two').add($('#two').siblings()).click(function(e){
         e.stopPropagation();
     });

Upvotes: 0

Related Questions