adding a reset button in javascript

6594 views javascript html
4

I am very green and starting to learn JS, just started my small project for practice. I found my "reset" button is no longer working, would you guys pls hv some comments? Thanks a lot.

<input type="button" value="Reset" onclick="clear()">
function clear(){
document.getElementById("result").value.reset();}

answered question

What is your "result" element?

FormElement.reset() resets a form, if that's what you want... or maybe you want something like document.getElementById('result').value = 'some value here';

Yea, Although my question is stupid but you guys are helpful, thanks.

2 Answers

7

Watch out, clear is already taken, you should rename your function:

function clearResult(){
  document.getElementById("result").value = ''
}
<input id="result" type="text" />
<input type="button" value="Reset" onclick="clearResult()">

See for example https://developer.mozilla.org/fr/docs/Web/API/Document/clear

posted this
9

Change

document.getElementById("result").value.reset()

to

document.getElementById("reset").reset();

And also, rename clear to some other function.

 function myFunc() {
            document.getElementById("reset").reset();
         }
    <form id="reset">
        Name<br><input type="text"  name=""><br>
        foo<br><input type="text" name=""><br>
         <input type="button" onclick="myFunc()" value="Reset">
      </form>

posted this

Have an answer?

JD

Please login first before posting an answer.