I have a semi-simple problem: I want my landing page to ask the user to enter their name into a input box. Once they have entered their name, the page should switch to another page that says Hello [username]... and present the content as well.
Here is my first html file (name.html) asking for the first name:
<body>
<form id="myForm" name="myForm">
<table class="input">
<tr>
<label>
<h1>Enter your firstname:</h1>
</label>
<td>
<input type="text" id="firstName" class="form-control" autofocus>
</td>
<td>
<input type="button" value="Submit" onclick="menuPage();" class="btn btn-primary btn-lg">
</td>
</tr>
<tr>
<td id="username">
</td>
</tr>
</table>
</form>
<script src="name.js"></script>
<script src="ie10-viewport-bug-workaround.js"></script>
</body>
Here is the accompanying JavaScript file (name.js):
function menuPage() {
var firstName = document.getElementById("firstName").value;
if (firstName.trim() && firstName != "") {
document.getElementById("username").innerHTML = firstName;
window.location.replace("menu.html")
} else {
alert("Please put in your first name.")
}
}
Now, here is the new html page (menu.html) that appears once the user enters their name:
<div class="jumbotron">
<div class="container">
<div>
<h1 class="display-3">Hello!</h1>
<h1 id="username"></h1>
<p>This is your weekly meal to cook! Enjoy!!</p>
</p>
<h2 class="quote"><span class="words"></span></h2>
<button class="btn btn-success btn-sm" value="Press Me to get your Weekly Menu">Change Meal</button>
</div>
</div>
</div>
<script src="name.js"></script>
<script src="menu.js"></script>
Lastly, the accompanying JS file (menu.js):
$(document).ready(function() {
weeklyMenu();
function weeklyMenu() {
var menu = ["meatloaf", "albondigas", "enchiladas", "lasgna", "chicken, mash potatoes, mac & cheese", "turkey burgers, sweet potatoes fries", "stuffed peppers", "french soup"];
var randomMenu = menu[Math.floor(Math.random() * menu.length)];
var splitMenu = randomMenu.split();
$('.words').text(splitMenu[0]);
}
$("button").on("click", function() {
weeklyMenu();
});
});