Can someone explain me why a function called "action" creates a type error in the following code as soon as the button is surrounded by the form tags. I assume this leads to a strange conflict with the form's action attribute, but i wonder why it happens in this scope ("action" is not defined in any other way):
<html>
<head>
<script type="text/javascript">
function action() {
alert('test');
}
</script>
</head>
<body>
<form>
<input type="button" value="click" onClick="action();">
</form>
</body>
</html>
To have a javascript function calling a screen action you must trigger a click on a button of the page or the event of the input. Put a button on the page an hide it using css (style="display: none;"). Name that button so that you can reference it latter, for instance 'MyButton'.
form action javascript. In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.
Named Functions: In JavaScript, named functions are simply a way of referring to a function that employs the function keyword followed by a name that can be used as a callback to that function. Normal functions with a name or identifier are known as named functions.
Inside the form, action
is a string reference to the form action. If you change your onclick to alert(action)
you will get the action of the form (which will be an empty string for your specific form).
In the same manner, form
will be a reference to a form, and method
will contain the form method, if you use them within the form. window.action
will still refer to your function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With