I am in a process of writing a hangman game and I bumped into a problem.
The problem is, I have a main.html page where I pick a category to start playing the game. When I click on a category it takes me to the new page. But, before going to the new page I have a function which randomly selects a word and create blank input spaces for the user to start guessing the word. But all this is getting refreshed when it takes me to the new page. I understood what my problem is but I am not sure how to solve it as I am in initial stages of learning javascript and html. I have added the code snippets below. Any help is appreciated. Thank you very much!
var hangman = {
sports: ['football','basketball','tennis','cricket','rugby','ping pong','badminton','hockey'],
animals: ['tiger','lion','leopard','wolf','dog','cat','monkey','donkey'],
countries: ['india','australia','america','england','new zeland','fiji','singapore']
};
var word;
var category;
//capture category list on HTML
//var categoryList = document.getElementById('category-list');
//var input = document.getElementById('toInput');
var keys = Object.keys(hangman);
//Grab the category being chose by the user
var selectCategory = function() {
var categoryList = document.getElementById('category-list');
//categoryList.addEventListener('click', function () {
if(event.target.tagName === 'LI') {
for(var i=0; i<keys.length; i++) {
if(keys[i] === event.target.innerHTML.toLowerCase()) {
category = keys[i];
}
}
word = hangman[category][Math.floor(Math.random()*hangman[category].length)];
}
createInput(word);
//});
}
var createInput = function(arg) {
var input = document.getElementById('toInput');
var newItem;
for(var i=0; i<arg.split('').length; i++) {
if(arg.split('')[i] !== ' ') {
newItem = document.createElement('input');
newItem.className = 'input-item';
input.appendChild(newItem);
}
else {
newItem = document.createElement('input');
newItem.className = 'empty-item';
input.appendChild(newItem);
}
}
window.location.assign("play.html");
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GoC</title>
<link rel="stylesheet" href="style-main.css" href="http://fonts.googleapis.com/css?family=Cinzel+Decorative">
</head>
<body>
<div class="wrap-main">
<h1 class="intro">GAME<span>oF</span>CAKES</h1>
<h2 class="tag-line">A CAKE FOR A WINNER!</h2>
<ul class="page-links">
<li><a href="player1.html">1 PLAYER</a></li>
<li><a href="player2.html">2 PLAYER</a></li>
</ul>
<div id="category">
<p>Pick a Category to start..</p>
<ul id="category-list" onclick="selectCategory()">
<li>Sports</li>
<li>Animals</li>
<li>Countries</li>
</ul>
</div>
</div>
<script type="text/javascript" src="hangman.js"></script>
</body>
</html>
But, before going to the new page I have a function which randomly selects a word and create blank input spaces for the user to start guessing the word. But all this is getting refreshedput the function in the new page then?createInputon next page inwindow.location(url?p1=value)and on new html you could read it back and do the necessary task with it. @mah3ndra