I am trying to put an inline onsubmit script on a form, but it's not working:
<!DOCTYPE html>
<html>
<head></head>
<body>
<form onsubmit="(function(event){console.log(event); return false;})(this);">
<button type="submit">Submit</button>
</form>
</body>
</html>
And is the parameter for the function the event or the form element?
I am trying to put an inline onsubmit script on a form, but it's not working:
<!DOCTYPE html>
<html>
<head></head>
<body>
<form onsubmit="(function(event){console.log(event); return false;})(this);">
<button type="submit">Submit</button>
</form>
</body>
</html>
And is the parameter for the function the event or the form element?
Share Improve this question asked Feb 22, 2016 at 23:01 poashoaspoashoas 1,8942 gold badges25 silver badges47 bronze badges 1-
In the inline JS,
this
is the form, not the event. – Barmar Commented Feb 22, 2016 at 23:02
1 Answer
Reset to default 8In inline Javascript, this
is always the element itself. event
is available as a variable local to the inline Javascript, so you can write:
<form onsubmit="return (function(event){console.log(event); return false;})(event);">
Since you're using an IIFE, its return
statement returns from the function, but not from the event handler. You need to return what the IIFE returns.
I'm not sure why you're using an IIFE, you can just write:
<form onsubmit="console.log(event); return false;">