JB Juliano
JB Juliano

Reputation: 632

jQuery conditional based on multiple selection

I have two IDs and I would like to create a condition based on my query.

$("#foo", "#bar").foo.bar({
  baz: function() {
    if(selector == "#foo") {
      console.log("foo");
    } else {
      console.log("bar");
    }
  }
});

Upvotes: 0

Views: 62

Answers (3)

Daerik
Daerik

Reputation: 4277

$("#foo, #bar").foo.bar({
  baz: function() {
    if(selector.prop('id') == "foo") {
      console.log("foo");
    } else {
      console.log("bar");
    }
  }
})

Upvotes: 2

Pinguto
Pinguto

Reputation: 436

You can go so well?

var x = $('.foo, .bar');
if(x.hasClass('foo')){
  // True!
} else {
  // false!
}

Upvotes: 1

Nhan
Nhan

Reputation: 3895

Simply separate into two statements:

$("#foo").foo.bar({
  baz: function() {
    console.log("foo");
  }
});

$("#bar").foo.bar({
  baz: function() {
    console.log("bar");
  }
});

Upvotes: 1

Related Questions