6

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>
3
  • 1
    I am surprised there is no duplicate for this question Commented Jun 2, 2016 at 11:20
  • That's the way inline event handlers work. See my answer here: stackoverflow.com/questions/6941483/onclick-vs-event-handler/… Commented Jun 2, 2016 at 13:37
  • @Rayon: this has been asked before, but I can imagine that it is difficult to name the question or what to look for without knowing what the issue is. I mean, the title of this question is not very indicative of the problem. Commented Jun 2, 2016 at 13:42

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ooh.. You are right! I did not change Load Type in Fiddle :(
Stupid me did not see that the call is the problem, not the function itself. Now everything makes sense again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.