External JS is added with <script src="..."></script>
and not with a <link>
element.
If you have run your code through a validator, it would have highlighted your error.
External JS is added with <script src="..."></script>
and not with a <link>
element.
If you have run your code through a validator, it would have highlighted your error.
Hey Try this code,
<html>
<head>
<title>Login Screen</title>
<link rel="javascript" type="text/javascript" href="login.js">
</head>
<body>
<div id="container">
<h1 onclick="loginDetails()">Login</h1>
<form>
<label for="username" class="label-1">Username</label><br>
<input type="text" id="username" class="text-box"><br>
<label for="password" class="label-2">Password</label><br>
<input type="text" id="password" class="text-box"><br>
<button type="submit" id="login" class="button">Sign In</button>
</form>
</div>
<script>
function loginDetails(){
alert('You Clicked on H1!')
}
</script>
</body>
Actually, your code works (current code snippet).
A better way would be by removing “onclick” attribute and creating event listener.
const h1 = document.querySelector('h1');
h1.addEventListener('click', loginDetails);
For some reason, my JavaScript function is not working. I want to use onclick on an h1 tag. On the snippet below, it works just fine. But when I try it on Chrome, it doesn’t do anything. Any help is appreciated.
function loginDetails() {
alert("Username: somebodynPassword: pass123");
}
<!DOCTYPE html>
<html>
<head>
<title>Login Screen</title>
<link rel="javascript" type="text/javascript" href="login.js">
</head>
<body>
<div id="container">
<h1 onclick="loginDetails()">Login</h1>
<form>
<label for="username" class="label-1">Username</label><br>
<input type="text" id="username" class="text-box"><br>
<label for="password" class="label-2">Password</label><br>
<input type="text" id="password" class="text-box"><br>
<button type="submit" id="login" class="button">Sign In</button>
</form>
</div>
</body>
</html>
Do you see anything in the developer console?
You put the click handler on the H1 tag, click the word “Login” above in your example and it works…
@skyline3000 — They already said that (including the fact it works in the live demo here) in the question.
@bjelleklang Uncaught ReferenceError: loginDetails is not defined at HTMLHeadingElement.onclick
This reads more like a game of spot-the-difference than an answer (and fails to remove the code which caused the problem in the first place).
i put all code in once because i think thats easy to copy and check for you. if this example is not working in your system then XD there is issue in your system.
“Actually, your code works (current code snippet).” — No, it doesn’t. Please note that the question explicitly says: “On the snippet below, it works just fine. But when I try it on Chrome, it doesn’t do anything.”.
Answers are supposed to answer the question, not