2

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

4

2 Answers 2

7

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Tried to give common class to each element as

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

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.