Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited body
Source Link
FluffyKitten
  • 14.4k
  • 10
  • 44
  • 54

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log("Player:" +option[UserOption-1] + " vs ConputerComputer:" + randomOption ); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log("Player:" +option[UserOption-1] + " vs Conputer:" + randomOption ); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log("Player:" +option[UserOption-1] + " vs Computer:" + randomOption ); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

typo & clearer console message
Source Link
FluffyKitten
  • 14.4k
  • 10
  • 44
  • 54

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log(randomOption"Player:" +option[UserOption-1] + " vs "+option[UserOption-1]Conputer:" + randomOption ); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log(randomOption + " vs "+option[UserOption-1]); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log("Player:" +option[UserOption-1] + " vs Conputer:" + randomOption ); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

added 4 characters in body
Source Link
FluffyKitten
  • 14.4k
  • 10
  • 44
  • 54

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name romfrom the options array like this:

console.log(options[UserOption]options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; console.log(randomOption) var UserOption = document.getElementById('UserSelect').value; console.log(option[UserOption]randomOption + " vs "+option[UserOption-1]); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name rom the options array like this:

console.log(options[UserOption]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; console.log(randomOption) var UserOption = document.getElementById('UserSelect').value; console.log(option[UserOption]); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

You just need to get the value of the select, like this:

var UserOption = document.getElementById('UserSelect').value; 

Then you can get the name from the options array like this:

console.log(options[UserOption-1]) 

Working Snippet

function Play() { var option = ["Rock", "Paper", "Scissors"]; var randomOption = option[Math.floor(Math.random()*option.length)]; var UserOption = document.getElementById('UserSelect').value; console.log(randomOption + " vs "+option[UserOption-1]); }
<body> <div class="form-group"> <select class="form-control" id="UserSelect"> <option value="1">Rock</option> <option value="2">Paper</option> <option value="3">Scissors</option> </select> </div> <button class="play" onclick="Play();">Play</button> </body>

Source Link
FluffyKitten
  • 14.4k
  • 10
  • 44
  • 54
Loading