I am building a joke website for some friends, and one page on the website I would like to have somewhat of an interactive game. I am used to C++ so the transition to javascript is an interesting one. All I have on the page for now is a paragraph element, a text input box and a button. When the button is clicked it is supposed to call the playGame() function. The playGame function looks at whats in the paragraph element ("game") and then look at the users input and generate the appropriate result. I am just using a set of conditionals to accomplish this but for some reason the function does not seem to change the paragraph element, thus making the entire thing a dud, of gone through and made some revisions but I have not been able to fix the issue. Here is the code:
<html>
<script>
function playGame()
{
test=document.getElementById("game");
var userInput=gameInput=document.getElementById("gameInput").value;
if (test.innerHTML=="Question_1")
{
if (userInput.toLowerCase()=="yes")
{
test.innerHTML="Ending_Result_1";
}
else if (userInput.toLowerCase()=="no")
{
test.innerHTML="Question_2";
}
else
{
test.innerHTML="Ending_Result_2";
}
}
else if (test.innerHTML=="Question_2")
{
if (userInput.toLowerCase()=="yes")
{
test.innerHTML="Positive_Ending";
}
else if (userInput.toLowerCase()=="no")
{
test.innerHTML="Ending_Result_3";
}
else
{
test.innerHTML="Ending_Result_4";
}
else
{
test.innerHTML="Refresh_page_message";
}
}
</script>
<head>
<title>CptTuna's Whip or No Whip</title>
</head>
<body>
<p id="game">"Question_1"</p>
<input id="gameInput" type="text">
<button type="button" onclick="playGame()">Answer</button>
</html>
I just changed the actual text content to generic phrases for the sake of appropriateness. Any help on why the function is not executing properly would be greatly appreciated.