redacted
redacted

Reputation: 453

JavaScript load different functions on different select options

I need JavaScript to call different functions on different select options. E.g.

<select name="city">
    <option value="1" onselect="function_one()">One</option>
    <option value="2" onselect="function_two()">Two</option>
    <option value="3" onselect="function_three()">Three</option>
    <option value="4" onselect="function_four()">Four</option>
</select>

Upvotes: 0

Views: 182

Answers (1)

Okan Kocyigit
Okan Kocyigit

Reputation: 13421

you should do that like this because onclick and onselect attributes are not supported for option tag.

<select name="city" onchange="selectionchanged(this)">
    <option value="1" >One</option>
    <option value="2" >Two</option>
    <option value="3" >Three</option>
    <option value="4" >Four</option>
</select>

<script type="text/javascript">

function selectionchanged(e) {
    if (e.value == "1") 
        function_one();
    else if (e.value == "2") 
        function_two();
    else if (e.value == "3") 
        function_three();
    else if (e.value == "4") 
        function_four();
}
</script>

DEMO

Upvotes: 1

Related Questions