Reputation: 1
I was trying to make a JavaScript program the will do the following work:
The problem I am facing:
After clicking on submit button the result disappears. (note: the username is visible for about 0.1 seconds);
What I want
<form action="" name="jsform">
<input type="text" name="uname" id="uname" placeholder="Your Name">
<input type="email" name="uemail" id="uemail" placeholder="Your Email">
<input type="password" name="upass" id="main_pass" placeholder="Enter A Password">
<input type="password" name="passconf" id="confirm_pass" placeholder="Confirm Password">
<h3 id="printUserName"></h3>
</form>
function validateForm() {
var userName = document.getElementById('uname').value;
document.getElementById('printUserName').innerHTML= userName + "is available" ;
console.log(userName);
return false;
}
Upvotes: 0
Views: 124
Reputation: 414
Attach your function to onSubmit event of your form and do your printing. Refer https://www.w3schools.com/jsref/event_onsubmit.asp
Don't forgot to add event.preventDefault() to the top of your function. Refer https://www.w3schools.com/jsref/event_preventdefault.asp
Upvotes: 0
Reputation: 12872
It disappears because the form is submitted to the same url essentially refreshing the page. You can prevent this binding a function to onsubmit
event and calling return false
or event.preventDefault()
from within.
You can show the data before submit by binding a function to the onchange
event.
Upvotes: 1
Reputation: 434
add submit handler to the form
<form action="" name="jsform" onsubmit="return validateForm()">
the form wont submit, if onsubmit returns false
Upvotes: 0