I'm using a select
. I want the last option
to call another page.
<select>
<option>Teste1</option>
<option>Teste2</option>
<option onclick="page.htm">Pagina</option>
</select>
Is this possible?
I'm using a select
. I want the last option
to call another page.
<select>
<option>Teste1</option>
<option>Teste2</option>
<option onclick="page.htm">Pagina</option>
</select>
Is this possible?
I suggest adding something in the element that indicates this extra functionality if only that element / option has a url.
You can always have something like
if (this.value == 'Pagina') window.location = 'page.htm';
But this semantically is not very correct. It would be best to add a data
to the element and use the onchange.
Using onchange:
var select = document.querySelector('select');
select.addEventListener('change', function () {
var selecionada = this.options[this.selectedIndex];
var url = selecionada.getAttribute('data-url');
if (url) window.location = url;
});
In this case the HTML would have <option data-url="/page.htm">Pagina</option>
.
I think what you're trying to do is onChange
, not onClick
. At least I've never seen a click event working for select
or option
.
You can do this:
document.querySelector('select').addEventListener('change', function(){
document.location.href = this.value;
});
See working on JSFIDDLE
If only the last value to redirect and if it form dynamic can do this:
<html>
<head>
<script>
function redirecionar(opc){
var total_options = opc.options.length;
var option_selecionada = opc.selectedIndex;
if(total_options == option_selecionada){
location.href = opc.options[opc.selectedIndex].value;
}
}
</script>
</head>
<body>
<select id="comboBreaker" name="comboBreaker" onchange="redirecionar(this)">
<option>Teste1</option>
<option>Teste2</option>
<option>Teste3</option>
<option value="page.htm">Pagina</option>
</select>
</body>
</html>