I am trying to build a simple calculator with Javascript and Im having issues when clearing display content.
Can someone please have a look at my code and let me know why it is not working.
Why won't setting the display value to an empty string work. What am I doing wrong? Tank you guys.
function testing(button){
var x = button.value;
document.getElementById("display").innerHTML+=x;
}
function clear() {
document.getElementById("display").innerHTML = "";
}
<body>
<input type="button" id="one" value="1" onClick="testing(this)">
<input type="button" id="one" value="2" onClick="testing(this)">
<input type="button" id="one" value="3" onClick="testing(this)">
<input type="button" id="clear" value="clear" onClick="clear()">
<h1 id="display"></h1>
</body>
I am trying to build a simple calculator with Javascript and Im having issues when clearing display content.
Can someone please have a look at my code and let me know why it is not working.
Why won't setting the display value to an empty string work. What am I doing wrong? Tank you guys.
function testing(button){
var x = button.value;
document.getElementById("display").innerHTML+=x;
}
function clear() {
document.getElementById("display").innerHTML = "";
}
<body>
<input type="button" id="one" value="1" onClick="testing(this)">
<input type="button" id="one" value="2" onClick="testing(this)">
<input type="button" id="one" value="3" onClick="testing(this)">
<input type="button" id="clear" value="clear" onClick="clear()">
<h1 id="display"></h1>
</body>
Share
Improve this question
edited Oct 13, 2017 at 8:36
Ole Haugset
3,8073 gold badges26 silver badges48 bronze badges
asked Oct 13, 2017 at 7:17
Kingsfull123Kingsfull123
5031 gold badge6 silver badges12 bronze badges
2 Answers
Reset to default 6Your method name is conflicting with the id value, just change it to clear1
and it should work.
function testing(button){
var x = button.value;
document.getElementById("display").innerHTML+=x;
}
function clear1(){
document.getElementById("display").innerHTML = "";
}
<body>
<input type="button" id="one" value="1" onClick="testing(this)">
<input type="button" id="one" value="2" onClick="testing(this)">
<input type="button" id="one" value="3" onClick="testing(this)">
<input type="button" id="clear" value="clear" onClick="clear1()">
<h1 id="display"></h1>
</body>
The problem is that there is a document.clear function which is taking precedence over your original call. You can test this by typing document.clear
into the console.
Try renaming your function to clearDisplay.
function testing(button){
var x = button.value;
document.getElementById("display").innerHTML+=x;
}
function clearDisplay(){
document.getElementById("display").innerHTML = "";
}
<body>
<input type="button" id="one" value="1" onClick="testing(this)">
<input type="button" id="one" value="2" onClick="testing(this)">
<input type="button" id="one" value="3" onClick="testing(this)">
<input type="button" id="clearDisplay" value="clear" onClick="clearDisplay()">
<h1 id="display"></h1>
</body>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744297849a4567350.html
评论列表(0条)