Select select select value and open different links by clicking the button

Asked

Viewed 80 times

0

Good afternoon, I am in need of a help, I need to select the value of the choice of a select and call a different link to value, by clicking the button.

<div>
   <button onclick="redirecionar()">COMPRAR</button>

   <select id="cor">
      <option value="1">BRANCO</option>
      <option value="2">PRETO</option>
      <option value="3">VERDE</option>
   </select>

   <script>
      var valor = document.getElementById('cor');
      function redirecionar() {

         window.location.href = "http://www.google.com";
      }
      if (valor == "2") {
         window.location.href = "http://www.facebook.com.br";
      }
      if (valor == "3") {
         window.location.href = "http://www.youtube.com.br";
      }

   }
   </script>
</div>

1 answer

1

Your code has some problems, you have to declare the if within the function, you have to take the property value of options, also has an extra key left in the code, and it is not much recommended to declare a script in a tag div:

var valor = document.getElementById('cor');

function redirecionar() {
  if(valor.value == "1") window.location.href = "http://www.facebook.com.br";
  else if(valor.value == "2") window.location.href = "http://www.youtube.com.br";
  else window.location.href = "http://www.google.com";
}
<div>
  <button onclick="redirecionar()">COMPRAR</button>

  <select id="cor">
    <option value="1">BRANCO</option>
    <option value="2">PRETO</option>
    <option value="3">VERDE</option>
  </select>
  
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.