How can I change the following to submit on both blur
and on submit
:
$('form').on('blur', 'input', function(event) { });
How can I change the following to submit on both blur
and on submit
:
$('form').on('blur', 'input', function(event) { });
You can pass multiple events to jQuery's on
method.
$('form').on('blur submit', function(event) { })
This would mean that whenever the $('form')
element was either a) blurred or b) submitted, the event handler would be invoked.
Change your function to a named function expression, then re-use the definition, like this:
var submitFunction = function(event) {
// stuff
}
$('input[type=text]').on('blur', submitFunction);
$('input[type=submit]').on('click', submitFunction);
Also, you don't need the middle argument to on()
- you can just use the $
function to target the elements you want. This means you won't add your click and your blur to all input
elements on the form.
you can trigger the submit in blur
$('form').on('blur', 'input', function(event) {
$('form').trigger('submit');
// or $('form').submit();
});
and for your form submit
$('form').on('submit', function(event) {
// when form submit ...
});