+ 2

Alert not working

Hi so this code was meant to show an alert message saying please enter a name if user doesnt enter a name.But once i dont enter a name it shows the inner html.Could it be that the else statement is wrong https://code.sololearn.com/WZweJ4x3kvHj/?ref=app

12th Apr 2018, 1:47 PM
idris
idris - avatar
4 Answers
+ 15
Glad to help. ^_^
12th Apr 2018, 2:08 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 13
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h2 id="Name"> </h2> <script> var Yourname=prompt("what is your name"); if(Yourname){ document.getElementById("Name").innerHTML="hello "+ Yourname; } else{ alert("please enter a name"); } </script> </body> </html>
12th Apr 2018, 1:50 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 13
Because when user enters nothing an empty string is returned. Yourname != null should be: Yourname
12th Apr 2018, 1:51 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
thank you
12th Apr 2018, 2:06 PM
idris
idris - avatar