admin管理员组文章数量:1315291
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 ments? Thanks a lot.
<input type="button" value="Reset" onclick="clear()">
function clear(){
document.getElementById("result").value.reset();}
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 ments? Thanks a lot.
<input type="button" value="Reset" onclick="clear()">
function clear(){
document.getElementById("result").value.reset();}
Share
Improve this question
asked Jun 30, 2020 at 4:39
willaimwillaim
231 gold badge1 silver badge3 bronze badges
3
- 2 What is your "result" element? – djcaesar9114 Commented Jun 30, 2020 at 4:41
-
FormElement.reset()
resets a form, if that's what you want... or maybe you want something likedocument.getElementById('result').value = 'some value here';
– StackSlave Commented Jun 30, 2020 at 4:42 - Yea, Although my question is stupid but you guys are helpful, thanks. – willaim Commented Jun 30, 2020 at 4:58
3 Answers
Reset to default 3Watch 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/fr/docs/Web/API/Document/clear
There are two ways to solve this error that I figured out. First thing if you want to use reset() then, use the following code:
<form id="result">
<input type="text">
<input type="button" value="Reset" onclick="clear1()"></form>
<script>
function clear1(){
document.getElementById("result").reset();}</script>
So if you see here, I have used the tag. Three errors I found in your markup were:
- clear() method is already reserved in javascript. clear() method in JavaScript is used for the removal of all the elements from a map and make it empty. So, instead use any other function name like clear1() just like I did.
- reset() function works for the tag. So instead of giving input tag an id give that id to the tag.
- You don't have to use the value in reset() method Use this method when you have a lot of input tags to perform with. Just wrap it with a tag.
Another method is easy when you have only one or two tags.
<input type="text" id="result">
<input type="button" value="Reset" onclick="clear1()">
<script>
function clear1(){
document.getElementById("result").value= ''}
</script>
Change
document.getElementById("result").value.reset()
to
document.getElementById("reset").reset();
And also, rename clear
to some other function as clear
is a reserved word and does something entirely different.
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>
本文标签: htmladding a reset button in javascriptStack Overflow
版权声明:本文标题:html - adding a reset button in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741961428a2407303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论