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>
The onClick attribute is an event handler that instructs the browser to run a script when the visitor clicks a button.
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.
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.
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.
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>
The scope that is created for executing the script that is passed to an onXXXX
attribute is defined in the HTML Standard:
scope
- Let realm be settings object's Realm.
- Let scope be realm.[[GlobalEnv]].
- 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.)
- If form owner is not null, then set scope to NewObjectEnvironment(form owner, true, scope).
- If element is not null, then set scope to NewObjectEnvironment(element, true, scope).
- 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>
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
.
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