views:

184

answers:

6

Hi,

I have the following drop-down menu:

<select name='type' id='type'>
    <option id='trade_buy' value='1' selected='selected'>Buy</option>
    <option id='trade_buy_max' value='1'>Buy max</option>
    <option id='trade_sell' value='2'>Sell</option>
    <option id='trade_sell_max' value='2'>Sell max</option>
</select>

I'd like jQuery to detect when the option with the id trade_buy_max is selected.

I've tried the following, but it doesn't seem to work.

$(document).ready(function() {

    $("option#trade_buy_max").select(function () {

        //do something

    });
});

Any ideas?

Thanks in advance.

A: 

Change .select to .change and put space before #

Amr ElGarhy
+2  A: 

you can bind change event on its select instead, then check if option selected

$("select#type").change(function () {
   if( $("option#trade_buy_max:selected").length )
   {
     // do something here
   }
});
Anwar Chandra
A: 

What you need to do is add an onchange handler to the select:

$('#type').change(function(){ 
  if($(this).val() == 2){
     /* Do Something */
  }
});
Justin Swartsel
I'd appreciate knowing -- why was this voted down?
Justin Swartsel
+1  A: 
$("option#trade_buy_max").change(function () {
    opt = $(this).children("option:selected").attr('id');
    if(opt == '#trade_sell_max'){
        // do stuff
    } 
});

Untested, but that should work.

inkedmn
+4  A: 

This works... Listen for the change event on the select box to fire and once it does then just pull the id attribute of the selected option.

$("#type").change(function(){
  var id = $(this).find("option:selected").attr("id");

  switch (id){
    case "trade_buy_max":
      // do something here
      break;
  }
});
Ryan
+1  A: 

Use the change event and get the id attribute of the selected option:

$('#type').change(function () {
  var selectedId = $('option:selected', this).attr('id');

  if (selectedId == "trade_buy_max") {
    // do something
  }
});
CMS