How Do I Get Form Submit Event Listener To Work In Js? February 26, 2024 Post a Comment I am replacing some jquery codes with vanilla js, and I am having trouble with the form submit event listener .submit() method on the HTMLFormElement:The submit event fires when the user clicks a submit button ( or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form.submit() method directly.- MDNYou can use the .requestSubmit() method instead, which does trigger the onsubmit event handler of the form and performs constraint validation on the form:/* vanilla js replacement */ myForm = document.querySelector("form[name=myForm]") myForm.querySelector("button").addEventListener('click', function() { myForm.requestSubmit(); }) myForm.addEventListener('submit', function(e) { e.preventDefault() console.log("Submitting form"); })Copy<formname="myForm"><inputtype="text"name="name" /><buttontype="button">Click me</button></form>CopyThe main downside to this method is that, unlike jQuery's .submit() method, requestSubmit() has weaker browser support. Share You may like these postsHow Can I Ignore Libraries Like Jquery When Profiling Javascript?Test Code Coverage Javascript Es6 Generators (redux-saga / Istanbul.js)Onclick Render New Component And Hide Current Component?Node.js Require Without Storing It Into A Variable Post a Comment for "How Do I Get Form Submit Event Listener To Work In Js?"
Post a Comment for "How Do I Get Form Submit Event Listener To Work In Js?"