Reputation: 893
I've written a dropdown menu that I want functions to fire when an option is clicked. It hasn't been working on any browser so I replaced the first function with a simple alert but that's not firing either. Any pointers would be appreciated!
<select name="webmenu" id="webmenu">
<option value="UK" id="UK_button" title="assets/images/uk_flag.png" onclick="alert('You are clicking on me');"> </option>
<option value="US" id="US_button" title="assets/images/us_flag.png" onclick="finalResultsUS()"> </option>
<option value="EU" id="EU_button" title="assets/images/euro_flag.png" onclick="finalResultsEuro()"> </option>
</select>
I've made a few changes to the code based on replies so far (thanks guys)
<select name="webmenu" id="webmenu">
<option value="UK" id="UK_button" title="assets/images/uk_flag.png" onchange="alert('You are clicking on me');"> </option>
<option value="US" id="US_button" title="assets/images/us_flag.png" onchange="finalResultsUS()"> </option>
<option value="EU" id="EU_button" title="assets/images/euro_flag.png" onclick="finalResultsEuro()"> </option>
</select>
<script>
document.getElementById("webmenu").onchange = function() {alert('you clicked me');
}
</script>
but I need a to be able to fire off a different function for each specific choice rather than one function for any change in selection. How could I do this without jquery please?
I could use:
document.getElementById("webmenu").onchange = function() {
alert(this.value)
return false
};
but how would I write it so that clicking on the US option (2nd option) would trigger the US function for example rather than just firing off alerts please?
Upvotes: 0
Views: 183
Reputation: 14353
Here is a solution with just javascript. You can change the code in each function to remove the alerts and replace with your working code.
<select name="webmenu" id="webmenu" onChange='changedFunction(this.value);'>
<option value="UK" id="UK_button" title="assets/images/uk_flag.png"> </option>
<option value="US" id="US_button" title="assets/images/us_flag.png"> </option>
<option value="EU" id="EU_button" title="assets/images/euro_flag.png"> </option>
</select>
changedFunction= function (val) {
//console.log(val);
switch (val) {
case 'US':
finalResultsUS();
break;
case 'UK':
finalResultsUK();
break;
case 'EU':
finalResultsEuro();
break;
}
};
finalResultsUS = function() {
alert('You clicked on US');
};
finalResultsUK = function() {
alert('You clicked on UK');
};
finalResultsEuro = function() {
alert('You clicked on EU');
};
Upvotes: 0
Reputation: 9145
I don't think click
event is valid on 'option'
all you can do is with change
$('#webmenu').change(function(){
var temp=$(this).children(":selected").val();
switch(temp)
{
case 'UK':
alert('Hello UK');
break;
case "EU":
alert("HEllo EU");
break;
}
});
Upvotes: 0
Reputation: 48
This simplified version of your code seems to work:
<select name="webmenu" id="webmenu">
<option value="UK" id="UK_button" onclick="alert('You are clicking on me');">UK-clickme</option>
</select>
What browser / version are you using?
Upvotes: 0
Reputation: 15387
You can not handle onclick
on option
You need to handle on select-> onchange
. Basic on Id call that functions
Upvotes: 1