1

Is it possible to call a JS function when a specific a item is selected from dropdownlist?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?
0

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 ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.