You can do this using native JavaScript. That is, without any external library, and without mixing JavaScript in HTML ( which is not very advisable ).
In this case you need to consider 3 things:
- What to put in HTML
- the event sink
- where to put JavaScript
In HTML the visible part is what is between >
and <
of each option
. What's in value
is what's important to the programming part. So instead of having the same in value
and the text of option
puts value
the URL you want. For example:
<option value="/Vilhena.html">Vilhena</option>
To select the element you can use:
var select = document.querySelector('select');
This will work fine if you only have 1 select on the page. If you have more than one you must give an ID to the select (the HTML would be: <select id="meuSelect">
) and you can select it like this:
var select = document.getElementById('meuSelect');
// ou assim:
var select = document.querySelector('#meuSelect');
Then add an event changer to it when you change:
select.addEventListener('change', function(){
var valor = this.value;
window.location.href = valor;
});
Finally puts the script at the bottom of the page, before the </body>
tag. This way JavaScript is read with the HTML already loaded into memory.