I have a form with text input where the user gives their name, and a submit input button. When the button is pressed, I want to save the name into a variable in JS to use later, and also change the Html page to the next page (next page is intro.html, I did create this file so that isn't the issue.). My code below doesn't work: it looks as if it just refreshes the original page. Please help! P.S. Sorry for poor organisation of code snippet, I'm new to StackOverflow.
var start = document.getElementById('menu-banner-button');
const toPage2 = function() {
var username = document.getElementById('username').value;
window.location.pathname = "resources/html/intro.html";
}
start.onclick = toPage2;
<!DOCTYPE html>
<html>
<head>
<title>MW - Main Menu</title>
<link href="resources/css/style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div class='menu-banner'>
<h1 class='menu-banner-text'>Hello... Welcome to My World.</h1>
<form>
<label class='name-form' for='username'>What shall we call you?</label>
<input class='name-form' type='text' id='username' name='username' maxlength="20">
<input class='name-form' type='submit' id='menu-banner-button' value="Let's Go!"></input>
</form>
</div>
<script type='text/javascript' src='./resources/js/index.js'></script>
</body>
</html>