Baumannzone
Baumannzone

Reputation: 780

jQuery multiple selectors same function

I have this jQuery Code:

$('#select-adults-room-1').change( function () {
  og.removeErrorsOcio();
});
$('#select-kids-room-1').change( function () {
  og.removeErrorsOcio();
});

What is the best way to do it? I know this looks weird, but not sure how to improve it if it's possible. I'm looking for some like this:

$('#select-adults-room-1','#select-kids-room-1').change( function () {
  og.removeErrorsOcio();
});

Thanks

Upvotes: 2

Views: 2015

Answers (2)

Ranjeet Singh
Ranjeet Singh

Reputation: 924

Tried to give common class to each element as

$('.your-element').change( function () {
  og.removeErrorsOcio();
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

You don't need to pass separate strings, place the comma between the selectors in a single string, like this:

$('#select-adults-room-1, #select-kids-room-1').change(function() {
    og.removeErrorsOcio();
});

Also note that you can pass the reference of the function directly to the change() method, like this:

$('#select-adults-room-1, #select-kids-room-1').change(og.removeErrorsOcio);

Upvotes: 7

Related Questions