tags:

views:

59

answers:

1

Using jQuery, how can I make it so that a function is called whenever any select object on the page is changed?

+2  A: 

You can bind the change event to all the select elements on the DOM:

$('select').change(function () {
  // do something
});

Inside the event handler, the this keyword refers to the select element triggered the event, and you can get it's attributes, eg. var selectId = this.id;

CMS
+1; Also, use `live(event, action);` if you need to use it for dynamically created objects.
Jeff Rupert
Thanks @Jeff, unfortunately the `change` event is not supported by `live`... yet... http://docs.jquery.com/JQuery_1.4_Roadmap#Events
CMS
That was my problem, I used that same code, but all my select object are dynamically created. Is there a way to still do this?
Brian
@Brian: How do you create your `select` elements?, the event can be bound just after they are inserted on the DOM...
CMS
I get them from ajax functions and insert them into the HTML of spans. How do I bind that event?
Brian