Where to add onsubmit to a form in Javascript

Jens

In my HTML I've got a form without an onsubmit event listener:

<form action="contact.php" method="post" id="contact">
    ...
    <input type="submit" value="Send" />
</form>

As a result, the form is always posted whenever I click the "Send" button. However, I'd like to validate the input first, so I'd like to attach an event listener and intercept the post. This is how I attach the event listener to the form:

window.onload = function() {
    function validate() {
        return window.confirm("Confirm");
    }
    var form = document.getElementById("contact");
    form.addEventListener("submit", validate);
}

While the event listener is being executed, if I go by above approach the form is always posted! However, if I make the validate() function global and use onsubmit="return validate();" in the <form> tag, then the form is only being submitted conditionally, as expected.

Why does this not work by adding the validate() function as above? It seems the false return value gets lost?

Quentin

Modern event handling has a more complex API, it gives more flexibility but that comes at the cost of not being able to tie behaviour to a simple boolean result.

For your use case, you need to capture the event object:

function validate(ev) {

Then you can prevent the default action on it:

if (!confirm('Confirm')) {
    ev.preventDefault();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Form Action & Javascript Onsubmit issue

From Dev

Javascript override form onsubmit event not working

From Dev

Onsubmit event after javascript form submit

From Dev

Simple form onsubmit not calling javascript function (JQuery)

From Dev

Asp.net add form onsubmit() from code behind

From Dev

JavaScript: Form onSubmit does not work when function is called 'submit'

From Dev

How do I validate this HTML/JavaScript form onsubmit?

From Dev

HTML form submit not calling Javascript "onsubmit" handler in Wordpress post

From Dev

javascript timeout function in onSubmit login-form for RememberMe

From Dev

How to give confirm dialog onsubmit of a form element using javascript function

From Dev

Javascript not receiving data from form onsubmit for Ajax call

From Dev

not working: <form onsubmit="alert(event.shiftKey)"> on javascript

From Dev

HTML button onClick listener calling HTML form onSubmit javascript function

From Dev

html for where to call onsubmit

From Dev

Form 'onsubmit' not getting called

From Dev

jQuery .validate() and form onsubmit

From Dev

Form.onsubmit not working

From Dev

form onsubmit ignores action

From Dev

Can I define a javascript variable that uses onsubmit in a form defining that form as a variable?

From Dev

Where do I add my ajax to Bootstrap 4's form validation javascript?

From Dev

'onsubmit' not running javascript function

From Dev

javascript onsubmit not working

From Dev

javascript function onsubmit not working

From Dev

javascript onsubmit not working

From Dev

javascript not working onsubmit

From Dev

Validation function in onsubmit form tag

From Dev

Form onsubmit being completely ignored

From Dev

Prevent form submission in the onsubmit event

From Dev

onsubmit and confirm always processing form?

Related Related

  1. 1

    Form Action & Javascript Onsubmit issue

  2. 2

    Javascript override form onsubmit event not working

  3. 3

    Onsubmit event after javascript form submit

  4. 4

    Simple form onsubmit not calling javascript function (JQuery)

  5. 5

    Asp.net add form onsubmit() from code behind

  6. 6

    JavaScript: Form onSubmit does not work when function is called 'submit'

  7. 7

    How do I validate this HTML/JavaScript form onsubmit?

  8. 8

    HTML form submit not calling Javascript "onsubmit" handler in Wordpress post

  9. 9

    javascript timeout function in onSubmit login-form for RememberMe

  10. 10

    How to give confirm dialog onsubmit of a form element using javascript function

  11. 11

    Javascript not receiving data from form onsubmit for Ajax call

  12. 12

    not working: <form onsubmit="alert(event.shiftKey)"> on javascript

  13. 13

    HTML button onClick listener calling HTML form onSubmit javascript function

  14. 14

    html for where to call onsubmit

  15. 15

    Form 'onsubmit' not getting called

  16. 16

    jQuery .validate() and form onsubmit

  17. 17

    Form.onsubmit not working

  18. 18

    form onsubmit ignores action

  19. 19

    Can I define a javascript variable that uses onsubmit in a form defining that form as a variable?

  20. 20

    Where do I add my ajax to Bootstrap 4's form validation javascript?

  21. 21

    'onsubmit' not running javascript function

  22. 22

    javascript onsubmit not working

  23. 23

    javascript function onsubmit not working

  24. 24

    javascript onsubmit not working

  25. 25

    javascript not working onsubmit

  26. 26

    Validation function in onsubmit form tag

  27. 27

    Form onsubmit being completely ignored

  28. 28

    Prevent form submission in the onsubmit event

  29. 29

    onsubmit and confirm always processing form?

HotTag

Archive