Jquery before submitting the form Jquery before submitting the form jquery jquery

Jquery before submitting the form


Add a submit listener to the form. When it's submitted, check to see if an element is selected, and if not you can prevent the submission by using return false. Here's an example:

$('#myForm').submit(function(){    if (/* test case not true */) {         $('#myError').show();        return false;     }    // ... continue work});

And here's the HTML:

<form id="myForm">    <input type="text"/>    <input type="submit"/></form>

If you don't want to use jQuery, you can also handle the submit event with plain JavaScript like so:

var myform = document.getElementById('myForm');myform.addEventListener('submit', function() { console.log('Submitted!'); return false; });