0
how come after i enter my input the page refreshes and goes blank again?
<!DOCTYPE html> <html> <head> <script> function outputname(){ var x,y,name; x = document.getElementById("form1"); y = x.elements["name"].value; document.getElementById("demo").innerHTML = "Hello "+ y +"<br>"; document.getElementById("demo").innerHTML += "How are you today" + y + "?"; } </script> </head> <body> <form id="form1"> Name:<input name="name" type="text" size="20"> <button onclick="outputname()">submit</button> </form> <p id="demo"></p> </body> </html>
3 Antworten
+ 3
2 main issues here..
• script tag should be placed in body right before closing body.
• submit event function needs to set preventDefault to prevent redirection of submit button.
+ 3
Just add
this.event.preventDefault();
into the event function.
0
@Calvin, it was originally in body just moved it for better reading on here, could you give me an example for the second point ?