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>

29th Jun 2017, 3:02 AM
Hoeft Brian
Hoeft Brian - avatar
3 odpowiedzi
+ 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.
29th Jun 2017, 3:09 AM
Calviղ
Calviղ - avatar
+ 3
Just add this.event.preventDefault(); into the event function.
29th Jun 2017, 4:58 AM
Calviղ
Calviղ - avatar
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 ?
29th Jun 2017, 3:31 AM
Hoeft Brian
Hoeft Brian - avatar