Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript function called "action"

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>
like image 694
needfulthing Avatar asked Jun 02 '16 11:06

needfulthing


People also ask

How do you call an action function in JavaScript?

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'.

What is JavaScript action function?

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.

What is named function in JavaScript?

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.


1 Answers

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.

like image 59
David Hedlund Avatar answered Oct 22 '22 23:10

David Hedlund