views:

582

answers:

2

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
deleting my lazy answer - +1 for code sample ;)
gnarf
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
I'm afraid that's just the nature of the "change" event...
J-P
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
A bit obtrusive! .... And won't degrade!
J-P