How to make the optgroup access a page

1

I have a code and I want it to work as follows: I created a list of options within a optgroup and I want it as soon as I click on an option, it goes to the page I want.

Example:

<optgroup>
<option>Opcao1</option>
<option>Opcao2</option>
<option>Opcao3</option>
</optgroup>

And once you click on option 2, for example, it would access the following url: www.site.com/lista/Opcao2/index.html

Then the default would be: www.site.com/lista/(opção escolhida)/index.html

The code is currently (so far):


Andthesitelookslikethis:

Would it be any way without using php?

    
asked by anonymous 13.07.2017 / 01:02

1 answer

0

In order to be interpreted in the click it is necessary to listen for the onchange event of the <select> tag, and when it happens we change from url by modifying window.location.href :

Example:

document.getElementById("cEst").onchange=function(){ 
  //this.value tem o valor que foi escolhido nas opções
  window.location.href = "www.site.com/lista/" + this.value + "/index.html"; 
}
<select name="tEst" id="cEst">
  <optgroup>
    <option>Opcao1</option>
    <option>Opcao2</option>
    <option>Opcao3</option>
  </optgroup>
</select>

I would advise you to be careful with the spaces in url because they are converted to other characters (usually + or% 20) and therefore may not work.

    
13.07.2017 / 02:50