Use button type ="reset"
and focus()
Hope this snippet will be useful
HTML
<form id = "myForm">
<input type = "text" id ="text1">
<input type = "text" id ="text2">
<input type = "text" id ="text3">
<button type = "reset" id = "restButton">Reset</button>
</form>
JS
var _getForm = document.getElementById("myForm");
_getForm.addEventListener('reset',function(){
document.getElementById("text1").focus()
})
Check this jsfiddle
EDIT
You can use firstElementChild
instead of using document.getElementById("firtChild")
.
Updated jsFiddle
Edit-2
If you have put your code inside head tag, you can use window.onload to attach eventlistener after window has loaded
window.onload =function(){
var _getForm = document.getElementById("your form id");
_getForm.addEventListener('reset',function(){
document.getElementById("text1").focus()
})
}
EDIT-3
With latest changes in original questions here are few updates
Note
- There is no form with
id=memberInfovar
. There is form with name="memberInfovar"
.So this _getForm = document.getElementById("memberInfo");
will throwaddEventListener of null
<input id="username" />
add type of this input, that is input type = "text"
<input type=button
. The type
must be wrapped inside double quotes, that is <input type="button">
Check this jsfiddle