JavaScript function called "action"

Can someone explain to me why a function called "action" creates a type error in the following code as soon as the button is surrounded by form tags. I suppose this leads to a strange conflict with the action attribute of the form, but I wonder why this happens in this area (the โ€œ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> 
+6
source share
1 answer

Inside the form, action is a string reference to the action of the form. If you change your onclick to alert(action) , you will get a form action (which will be an empty string for your specific form).

In the same way, form will be a reference to the form, and method will contain the form method if you use them in the form. window.action will still reference your function.

+5
source

All Articles