0

Is there a way to combine these jQuery calls? No real reason other than to tidy up my code a little.

$("#myForm input").blur(function() {
 saveForm();
});
$("#myForm :radio, #myForm :checkbox").click(function() {
 saveForm();
});
asked Oct 31, 2016 at 4:31
6
  • And how do you think these could be chained? The selected elements are different... Commented Oct 31, 2016 at 4:33
  • I'm not sure, hence the question. I just want to see if there's a single way of calling saveForm() rather than with these two seperate functions. Commented Oct 31, 2016 at 4:35
  • 2
    Use change event on input elements. Commented Oct 31, 2016 at 4:36
  • Thanks @Tushar, as you suggested, change event is the way to go. $("#myForm input, #myForm :radio, #myForm :checkbox").change(function() { saveForm(); }); Commented Oct 31, 2016 at 4:47
  • .change(saveForm) is a lot tidier. There's no need for the anonymous function if all it does is call saveForm() with no arguments. Commented Oct 31, 2016 at 5:04

1 Answer 1

1

use like below for click and blur

$('#myForm input, #myForm :radio, #myForm :checkbox').on('click blur', function () {
 saveForm();
});
answered Oct 31, 2016 at 4:40
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mani, @Tushar made a similar suggestion in the comments to my original question which is a bit cleaner than this solution.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.