I have a problem guys. Trying to write HTML and JS code in different files and connect them. I think I have done it, but nothing works. How it must be done? (javascript file name is correct)
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="index.js">
<title>Document</title>
</head>
<body>
<h3>Check password size</h3>
<div class="checker">
<input type="password" name="passcode" placeholder="Enter password" ><br><br>
<input type="submit" value="check my password!" onclick="checkpassword()">
</div>
<p id="message" class="message"></p>
</body>
</html>
JAVASCRIPT
function checkpassword(){
var pas=document.getElementsByName('passcode')[0].value;
var x=pas.length;
document.getElementById("message").innerHTML="Password size is" +" " +x +" " +"characters";
}
You sould use the following line to call for/import your javascript.
<script type="text/javascript" src="index.js"></script>
The way you are writing the code is only to call for a css file. To call for a different language, you use a different code.
<script src="fileName.js"></script>
This is used to connect js to html src means source One more way to do js in html is by using in html and writing your code in the tag like
<script>
function checkpassword(){
var pas=document.getElementsByName('passcode')[0].value;
var x=pas.length;
document.getElementById("message").innerHTML="Password size is" +" "
+x +" " +"characters";
}
</script>
you can put this code in html or can make seprate files
hope it helped you :)
You need to use the script tag to connect javascript with html
<script src="yourFile.js"></script>