Detect when a specific <option> is selected with jQuery

Philip Morton picture Philip Morton · Dec 23, 2009 · Viewed 37.6k times · Source

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.

Any ideas?

Answer

Ryan picture Ryan · Dec 23, 2009

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