views:

85

answers:

3

So this is using javascript and HTML.

For example:

   <select name='test' >
            <option value='1'>
            <option value='2'>
            <option value='3'>
    </select>

If someone chooses option value 2 (from the dropdown) I want a popup to appear. However if they choose option value 1 or option value 3 (from the dropdown) I want nothing to happen.

How can I do this?

Thanks

A: 

You can do an event handler like this:

<script type="text/javascript">
function pop(a) {
    if (a.value==2) alert('two');
}

</script>

and for the html:

<select id="sel" onchange="pop(this)">
<option value="1">one</option>
<option value="2">two</option>

edl
A: 

With something like this:

<select name='test' onchange='if(this.value==2) alert("TEST")'>
    <option value='1'>
    <option value='2'>
    <option value='3'>
</select>
Marc
+3  A: 

Add an id to the select (<select name='test' id='test'>). Then add (after the <select>):

<script>
document.getElementById("test").onchange = function(){
    if (this.options[this.selectedIndex].value == '2') {
        alert('hello world!');
    }
}
</script>
Flavius Stef
+1 for separating the controller (javascript) from the Model/View (HTML)
Stephen P