We are trying to make a general approach for the part of the software that we develop that are related to the form fields.
So far, so good, but we are working with an edge that prevents the submission of the form / field to which another handler is bound.
Here's a (concise) use case:
HTML:
<form id="form1"> <input type=field id="field1"/> </form> <a href="#" id="link">click to submit</a>
The normal behavior is that when the user enters “foo” into the field and enters it, the form is processed and sent to the correct “endpoint”, which is not necessarily defined in the form’s opening tag. There may be some function (from somewhere else) that handles this event-event.
Unfortunately, we cannot predict what this function is, we would like to keep it universal.
In the above HTML, clicking on the link should trigger an event-event in the form field, which mimics the behavior of the browser / user and, therefore, an unknown handler.
This is our Javscript (we use jquery):
$('#field1').keypress(function (event) { if (event.which == 13) { console.log("enter pressed"); //return false; only if needed } }); $( "#link" ).click(function() { var e = jQuery.Event('keypress'); e.which = 13; //
When you enter "foo" in the field and click, enter the form to be submitted. But when we click on the link, we do focus (), and then trigger the key event, but the form is not submitted .
We cannot use submit () due to unknown handlers.
Try the code here: http://codepen.io/conversify/pen/yOjQob
javascript jquery html forms
unicorn80
source share