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
2009-12-29 06:22:43
+1; Also, use `live(event, action);` if you need to use it for dynamically created objects.
Jeff Rupert
2009-12-29 06:25:20
Thanks @Jeff, unfortunately the `change` event is not supported by `live`... yet... http://docs.jquery.com/JQuery_1.4_Roadmap#Events
CMS
2009-12-29 06:26:14
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
2009-12-29 06:29:24
@Brian: How do you create your `select` elements?, the event can be bound just after they are inserted on the DOM...
CMS
2009-12-29 06:34:18
I get them from ajax functions and insert them into the HTML of spans. How do I bind that event?
Brian
2009-12-29 07:03:40