Link a Javascript in my HTML | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Link a Javascript in my HTML

My code var clave = prompt ("What is the password"); //Prompt to ask password if(password == 20 ) // password { //if pássword is correct document.getElementById('result').innerHTML ="<h1> Welcome!</h1>"; } //false else { document.getElementById('result').innerHTML ="<h1>You are wrong !</h1>"; }; I used the code inside the body and everything worked. I tried to link the jvscript but only the prompt worked, didnt add the if or else text. Anyone could guide me? I am looking to work in a simple password without databases get. Make a javascript in another file with a password and link. Just to experiment, thank you.

7th Jan 2017, 1:42 PM
alvaro mesa
alvaro mesa - avatar
3 Answers
+ 7
document.getElementById works properly only when page is fully loaded…… try this : window.onload = function(){ //all code goes here };
7th Jan 2017, 1:55 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 5
You seems to link your js inside the <head> element... @ValentinHacker solution works, but you can also simply insert the link to your js file in the <body> ;) ( the content of the file linked is placed where the <script src=""></script> is, as it was already present in the html source: so, inside <head> tag, when loaded/executing, the <body> and its childs don't already exists for your code. Inside <body>, your code need to be placed AFTER the elements you need to access through your script ^^ )
7th Jan 2017, 2:21 PM
visph
visph - avatar
+ 1
Awesome I made it here in sololearn at it works I will try your answers in my github later. Great community!
7th Jan 2017, 3:22 PM
alvaro mesa
alvaro mesa - avatar