Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call a function named clear from an onclick attribute?

i'm trying to create a simple calculator, when a button is clicked its value is shown in the text field and the button "C" should clear the text field but its onclick="clear()" is not working??

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Calculator</title>
        <style>
            #button{
                padding: 10px;
            }
        </style>
        <script>
            function fill(val){
                document.getElementById("field").value+=val;
            }
            function clear(){
                document.getElementById("field").value="";
            }
        </script>
    </head>
    <body>
        <form>
        <table>
            <tr><input type="text" name="field" id="field" size="8"/></tr>
        <%! int k = 1; %>
        <% for(int i=0;i<3;i++){ %>
        <tr> <%
            for(int j=0;j<3;j++){ %>
            <td><input type="button" id="button" onclick="fill(this.value)" value="<%=k++%>" /></td>
          <%  } %>
        </tr>
        <% } %>
        <tr>

<!--here onclick="clear()" is not working?? -->

            <td><input type="button" id="button" value="C" onclick="clear()"/></td>
            <td><input type="button" id="button" value="0" onclick="fill(this.value)"</td>
            <td><input type="submit" id="button" value="="</td>
        </tr>
        </table>
        </form>
    </body>
</html>
like image 627
Arpit Tomar Avatar asked Jul 24 '15 14:07

Arpit Tomar


People also ask

What is an onclick attribute?

The onClick attribute is an event handler that instructs the browser to run a script when the visitor clicks a button.

What is HTML onclick?

The onclick event attribute in HTML works when the user clicks on the button. When the mouse clicked on the element then the script runs. Syntax: <element onclick = "script"> Attribute Value: This attribute contains a single value script that works when the mouse clicked on the element.


3 Answers

Intrinsic event attributes (like onclick) are horrible. Internally they implement with:

Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues.

Consequently, you are actually calling document.clear() instead of your global clear().

The quick fix for this is to rename the function to something else or explicitly call window.clear().

The better solution is to bind your event handlers with addEventListener instead of intrinsic event attributes.

enter image description here

like image 159
Quentin Avatar answered Oct 03 '22 21:10

Quentin


Your HTML has problems, but the reason your function is not working is because of its name "clear", which is already defined by document.clear().

Change the name of that function to something like clearField() and it will work.

like image 37
Mychal Hackman Avatar answered Oct 03 '22 23:10

Mychal Hackman


clear is a reference to a (deprecated) document.clear function. This has precedence to (shadows) the function you created, because of the scope that is created specifically for onXXXXX attributes in HTML.

This peculiar scope is only active in the script that is provided to this attribute. As soon as execution moves into the JavaScript code that is defined in script tags, this scope "overload" is gone.

So for instance, your code would work again, if you would first call another function, and then let that function call your clear function. Like so:

function clear() {
    alert("my clear() was called");
}

function helper() {
    clear();
}
<button onclick="helper()">Clear!</button>

Background / Source

The scope that is created for executing the script that is passed to an onXXXX attribute is defined in the HTML Standard:

scope

  1. Let realm be settings object's Realm.
  2. Let scope be realm.[[GlobalEnv]].
  3. If eventHandler is an element's event handler, then set scope to NewObjectEnvironment(document, true, scope). (Otherwise, eventHandler is a Window object's event handler.)
  4. If form owner is not null, then set scope to NewObjectEnvironment(form owner, true, scope).
  5. If element is not null, then set scope to NewObjectEnvironment(element, true, scope).
  6. Return scope.

These steps describe how the scope is defined, and this includes which identifiers are bound to which properties. And so we see in step 3 that document is added to the scope, which means that document properties become accessible via global names. Similarly, the scope may be extended further. For instance, in step 5, the element's properties also become accessible as globals.

Here is an example of that latter point:

var id = 12; // global variable
console.log(id); // 12
<button id="this-is-me" onclick="console.log(id)">click me</button>

Conclusion

This scope issue can be confusing, certainly when document is an object with many (and many obscure) properties.

All the more reason to avoid using onXXX attributes in your HTML. Best practice is to add event handlers in the main script, using addEventListener.

like image 20
trincot Avatar answered Oct 03 '22 23:10

trincot