As I delve into the world of front-end web development, I decided to challenge myself with a little exercise by creating a basic HTML form.
<!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">
<title>Document</title>
</head>
<body>
<form id="form">
<label for="name">Name:</label><br>
<input type="text" id="name"><br>
<label for="pass">Password:</label><br>
<input type="password" id="pass"><br><br>
<input type="submit" value="Submit">
</form>
<script src="example.js"></script>
</body>
</html>
Here's the JavaScript code I'm using for the form:
const form = document.getElementById('form')
const formName = document.getElementById('name')
const formPass = document.getElementById('pass')
const yourName = "John"
const yourPass = "Doe"
form.addEventListener('submit', function(e){
e.preventDefault()
if (formName.value == yourName && formPass.value == yourPass) {
document.querySelector('loginName').innerHTML = yourName;
window.location.href = 'login.html';
}
})
My aim is for the webpage to redirect to a login.html
after submitting the form. The login.html
only contains:
<!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">
<title>Document</title>
</head>
<body>
<h1 id="loginName"></h1>
</body>
</html>
However, I'm facing an issue with adding the variable yourName
from JavaScript to the h1
tag in the login.html
.