user5066670
user5066670

Reputation:

Script is not executing and script not working

I am new to coding, and I trying to create a 'reservations form' to allow people to fill in their details to reserve a game. I have put together some script to display the name and ID of the game when the user clicks the button.

But (guess what!) it does not work.

Help will be greatly appreciated. Thank you.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Games Library Reservations Page</title>
</head>
<body>
    <div>
        <h1>Reservations</h1>
    </div>

  <p><b>Wish to reserve a game? Just fill in this form:</b> </p>

  <form name="reservationsForm">

      <p><strong>Game ID</strong><br>   
      <input type="number" name="gameID" placeholder="1-8">

    <button type="button" onclick="gameConfirmation()">Reserve a game!</button>
    <p id="gameChosen"></p>

</form>
</div>

<script>
function gameConfirmation()
    var id;

    id = document.getElementById("gameID").value;

    if (id == 1) {
    document.getElementById("gameChosen").innerHTML = "You have selected to ``reserve game ID 1: Fantasy World.";
    } else if (id == 2) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 2: Sir Wags A Lot.";
    } else if (id == 3) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 3: Take A Path.";
    } else if (id == 4) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 4: River Clean Up.";
    } else if (id == 5) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 5: Pinball.";
    } else if (id == 6) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 6: Ghost Girl.";
    } else if (id == 7) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 7: Dress Up.";
    } else if (id == 8) {
    document.getElementById("gameChosen").innerHTML = "You have selected to reserve game ID 8: Where is my hat?.";
    } else {
    document.getElementById("gameChosen").innerHTML = "There is no game with that ID. Please try again.";
    }
}
</script>
</div>

</body>

</html>

Upvotes: 0

Views: 29

Answers (1)

guergana
guergana

Reputation: 374

If you call getElemtentByID you need to set an ID as well, add <input type="number" name="gameID" id="gameID" placeholder="1-8"> and well yes, you are missing a { after the declaration of your function.

Upvotes: 3

Related Questions