html - Javascript calculator simple clear feature not working - Stack Overflow

I am trying to build a simple calculator with Javascript and Im having issues when clearing display con

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
Add a ment  | 

2 Answers 2

Reset to default 6

Your 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信