Is it possible to call a JS function when a specific a item is selected from dropdownlist?
+1
A:
HTML:
<select id="menu" name="menu">
<option value="something">Click here</option>
<option value="nothing">Not this</option>
</select>
JS:
document.getElementById('menu').onchange = function() {
if (this.options[this.selectedIndex].value === 'something') {
// Do something
}
};
Change "something" to whatever value you want.
J-P
2009-07-18 10:09:02
deleting my lazy answer - +1 for code sample ;)
gnarf
2009-07-18 10:13:16
one more little question, with onchange event there is a problem with re-selecting same item. If I try to select the same item event, the function won't get triggered. How to fix that?
markiz
2009-07-18 11:30:10
I'm afraid that's just the nature of the "change" event...
J-P
2009-07-18 15:18:19
A:
Another solution would be:
HTML & JS:
<select id="menu" name="menu" onClick="eval(this.value);">
<option value="func1();">Click here</option>
<option value="func2();">Not this</option>
</select>
you can change 'onClick' with 'onChange' if you want.
i borrowed some code from J-P ;-)
Chris
2009-07-18 10:34:07