0

I want different behaviors depending on which <option> element is selected:

<select name="colorSelector" onchange="handleColorChange();">
    <option value="">- select -</option>
    <option value="1">Red</option>
    <option value="2">Blue</option>
</select>

function handleColorChange() {
    // Behave different depending on which <option> was just selected
}

What do I need to pass into the handleColorChange() method from inside the onchange listener in order to accomplish this?

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

5 Answers5

2

Looks like noone has mentioned switch yet. Also, don't put the function in the HTML, that's just bad. Here is the right way:

document.getElementsByName( 'colorSelector' )[ 0 ].onchange = function ( ) {
    switch ( this.options[ this.selectedIndex ].value ) {
        case '1':
            // Do something when "Red" is selected
            break
        case '2':
            // Do something when "Blue" is selected
            break
    }
}
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • if it's in a form he can also use `document.forms[0].elements['colorSelect'].onchange` (assuming it's the first form on the page... or if you would prefer you can give your form a `name` and call it using that. – rlemon Apr 19 '12 at 17:09
  • I know. I was just adding that info ***in case*** the OP *does have it available* :P cheers – rlemon Apr 19 '12 at 17:25
1

Try

function handleColorChange(self)
{
   console.log(self.selectedIndex);
   console.log(self.options[self.selectedIndex].text);
   console.log(self.options[self.selectedIndex].value)
};

And in HTML code

<select id="mySelect" name="colorSelector" onchange="handleColorChange(this);">
Mike
  • 1,042
  • 1
  • 8
  • 14
0

you dont have to pass anything to function, just assign an id to select:

<select id="mySelect" name="colorSelector" onchange="handleColorChange();">
    <option value="">- select -</option>
    <option value="1">Red</option>
    <option value="2">Blue</option>
</select>

function handleColorChange() {
var x=document.getElementById("mySelect").selectedIndex;
var y=document.getElementById("mySelect").options;
alert("Index: " + y[x].index + " is " + y[x].text);
}

Or If you want to get by name then do this in your function:

var x=document.getElementsByName("colorSelector")[0].selectedIndex;
var y=document.getElementsByName("colorSelector")[0].options;
alert("Index: " + y[x].index + " is " + y[x].text);
Habib
  • 219,104
  • 29
  • 407
  • 436
0

use this

<select name="colorSelector" onchange="handleColorChange(this);">
    <option value="">- select -</option>
    <option value="1">Red</option>
    <option value="2">Blue</option>
</select>

function handleColorChange(element) {
    // Behave different depending on which <option> was just selected
}
Imre L
  • 6,159
  • 24
  • 32
0
<select name="colorSelector" onchange="handleColorChange(this);">
    <option value="">- select -</option>
    <option value="1">Red</option>
    <option value="2">Blue</option>
</select>​


function handleColorChange(obj) {
    alert(obj.options[obj.selectedIndex].value);
}​

JSFiddle here

Christian
  • 1,258
  • 10
  • 11