javascript onsubmit not working

Stoffl

I am trying to make a javascript function work on submitting the form, the function doesnt seem to run. Can anyone help?

<html>
<head>
    <script>
        function upload(){
                alert("I am an alert box!");
        }
     </script>
</head>
<body>
    <form enctype="multipart/form-data" method="post" onsubmit="return upload();">
    <input type="file" name="file">
    <input type="submit" name="upload" value="Datei hochladen">
    </form>
</body>
</html>
adeneo

When attaching the event handler to the form element, the scope of the event handler is the form and not the window

<form enctype="multipart/form-data" method="post" onsubmit="return upload(this);">

<script>
    function upload(scope) {
        console.log(scope); // The passed scope from the event handler is
    }                       // the form, and not window
</script>

As input elements inside a form are attached as properties to the form object, where the name is the key, calling upload() in the event handler, where the scope is the form, would equal calling form.upload(), but the form already has an element with that name so form.upload is the upload button, not the upload() function in the global scope.

To solve it, either rename the function or the element

<html>
<head>
    <script>
        function upload(){
                alert("I am an alert box!");
        }
     </script>
</head>
<body>
    <form enctype="multipart/form-data" method="post" onsubmit="return upload();">
    <input type="file" name="file">
    <input type="submit" name="upload2" value="Datei hochladen">
    </form>
</body>
</html>

FIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

javascript onsubmit not working

From Dev

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

From Dev

javascript onsubmit change field value

From Dev

R Tcl/Tk: OnReset not working after OnSubmit

From Dev

AJAX not working with onsubmit

From Dev

Javascript override form onsubmit event not working

From Dev

JavaScript onsubmit preventDefault not working conditionally

From Dev

Onsubmit event after javascript form submit

From Dev

React tutorial: CommentForm's onSubmit not working

From Dev

Calling multiple Javascript functions onSubmit

From Dev

React Form Component onSubmit Handler Not Working

From Dev

jquery onsubmit return false not working

From Dev

'onsubmit' not running javascript function

From Dev

Form validation not working using onsubmit

From Dev

Where to add onsubmit to a form in Javascript

From Dev

onsubmit validation option of infragistics igeditor not working

From Dev

javascript, ajax, PHP insert onSubmit not working...POST not set

From Dev

javascript function onsubmit not working

From Dev

Javascript difference between onChange and onSubmit

From Dev

Form Action & Javascript Onsubmit issue

From Dev

javascript onsubmit change field value

From Dev

Why is my form validation not working (onsubmit not triggering)?

From Dev

Form.onsubmit not working

From Dev

onsubmit="function()" is not working

From Dev

JavaScript onsubmit preventDefault not working conditionally

From Dev

Webform onSubmit not working with more that on input in React

From Dev

javascript not working onsubmit

From Dev

Form onsubmit="" with confirm window not working

From Dev

Form attribute "onsubmit" not working properly

Related Related

HotTag

Archive