开发者

Javascript how to make a reset button

开发者 https://www.devze.com 2023-03-17 23:14 出处:网络
Once a user enters lets say 5 numbers into that list I want to 开发者_运维百科have a reset button so if the user wants to delete them all instantly they can click reset which should delete the entire

Once a user enters lets say 5 numbers into that list I want to 开发者_运维百科have a reset button so if the user wants to delete them all instantly they can click reset which should delete the entire list....

I have tried making a reset button with a reset function that fetches the element by id and tries to delete it but it did not quite work so I am hoping someone here knows how to do that...

here is the fiddle http://jsfiddle.net/bikbouche1/QVUHU/67/


Don't forget that you could always just use a simple

<input type="reset" value="Reset">

if you want to clear the entire form. Note that you'll need to move those inputs that are currently outside of the form tags in your fiddle to within the form tags for this to work.

Here's an updated fiddle.

Also note that nrabinowitz's answer won't work for two reasons. First of all, the incorrect ID was given ("numbers" instead of "number") and also, .innerHTML will not work for an input field. Instead you should use:

document.getElementById("reset").onclick = function() {
  document.getElementById("number").value = "";
};

An updated, working version of his fiddle is here.


See this updated fiddle. Key code:

document.getElementById("reset").onclick = function() {
   document.getElementById("numbers").innerHTML = "";
};

To clarify, since this appears to have confused other posters - the "numbers" div is a placeholder for one or more dynamically-created input elements, not a reference to the original "number" input. By setting innerHTML to "", the above code deletes the dynamically created elements entirely - it's not trying to clear their values.

0

精彩评论

暂无评论...
验证码 换一张
取 消