How can I submit a form when a field (In this case the form only has one field) loses focus?
I tried this, but it's not working:
$("form").submit();
UPDATE
I forgot to mention that the form was created with jquery as well:
$("div").html('<form action="javascript:void(0)" style="display:inline;"><input type="text" value="' + oldValue + '"></form>');
This is probably why it won't submit, I think it's because the events aren't being observed.
jQuery focusout() MethodThe focusout() method attaches a function to run when a focusout event occurs on the element, or any elements inside it. Unlike the blur() method, the focusout() method also triggers if any child elements lose focus. Tip: This method is often used together with the focusin() method.
The focusout event fires when an element has lost focus, after the blur event. The two events differ in that focusout bubbles, while blur does not. The opposite of focusout is the focusin event, which fires when the element has received focus.
The blur() method removes focus from an element.
The onfocusout event occurs when an element is about to lose focus. Tip: The onfocusout event is similar to the onblur event. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child loses focus, you should use the onfocusout event.
$('form :input').blur(function() {
$(this).closest('form').submit();
});
Trigger the form's submit() event when the field loses focus. You can detect this by attaching a blur()
event handler to it.
$("#field").blur(function() {
$("#form").submit();
});
If you field doesn't have an ID or other means of easily identifying it (which I would recommend) you could also do something like this:
$("#form :input").blur(function() {
$("#form").submit();
});
since there's only one field.
What about this
var $yourForm = $('#form');
$yourForm.find('input').eq(0).blur(function() {
$yourForm.submit();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With