You can get the required result using select’s onchange event.
If the parameter required for your function is the select value, you can access it using this.value
in the body of the function (as long as it has been executed within the scope of select).
If you want to set different parameters for each option, but not related to option value, you can use the attributes prefixed with data-
as in the example:
<select id="select">
<option data-param="parametro-1">1</option>
<option data-param="parametro-2">2</option>
</select>
<p id="value"></p>
<p id="param"></p>
<script type="text/javascript">
(function () {
document.getElementById('select').addEventListener('change', function() {
var selectedOption = this.children[this.selectedIndex];
var value = this.value;
var param = selectedOption.getAttribute("data-param");
document.getElementById('value').textContent = 'value = ' + value;
document.getElementById('param').textContent = 'data-param = ' + param;
})
})();
</script>
Can you explain your question better? You want an event to be triggered when the user clicks on some option of a select?
– Giovane
If you want to execute a function when selecting a select option, use the onchange in select, when there is change in select it triggers the function.
– Eduardo Breno
@Giovane, exactly, open a function of select by clicking on option, already tested with onchange and did not give....
– Alh
@Eduardobreno already tested onchange and did not give..
– Alh
@What kind of function? The
Eduardo Breno
This right, you must useonchange
when in aselect
. Show here which function you want to perform, explain better everything we can see how to make it work with theonchange
.– Giovane
@Giovane, I want to open Onclick to do the same function as the onclick links here: https://jsfiddle.net/pfmfoe30/14/
– Alh
@What the function should do?
– Giovane
@Giovane, in js fiddle you can check, by clicking on "span1" note that opens a specific div, and on div "span2" and "span3" as well..
– Alh