JQuery live or something similar with .change()?
Solution 1
Note: jQuery 1.4 now supports the live function for all normal events. It didn't work with IE8 until recently, but I believe this is fixed with jQuery 1.4.2. See this resolved jQuery ticket: IE8 DOES NOT SUPPORT THE CHANGE EVENT WHILE USING LIVE
Solution 2
LiveQuery plugin supports all events.
Solution 3
Ok, after Funka's comments on my first attempt at answering my own question, I now have this:
$('.foo').change(function test(){
$(fu).prependTo("#some-div").bind("change", test)
$(this).unbind("change",test)
};
Which will bind the function to each element as it is created, and unbind it from the one created before it. This solves my problem UI-wise, but I'm obvs novice so am really open to learning if I am missing something again! ;)
Solution 4
Oh that wasn't so bad, i just wrapped it in a live on click event and it worked just fine.
$("#foo").live("click", function(){
$('.fu').change(function(){
blah blah blah
});
});
Greconomist
Updated on June 29, 2022Comments
-
Greconomist almost 2 years
I want to do this: http://docs.jquery.com/Events/live#typefn
Only .live() doesn't support the change event- any ideas for work arounds?
Need to bind a function to some on-the-fly DOM elements, but not until change.
-
Funka over 14 yearsYou'll probably find that this has the (unwanted) effect of binding and re-binding your change function multiple times...
-
Greconomist over 14 yearshm- i hadn't thought of that but you are obviously right. Since I won't need the function to work for the clicked/changed element again, would an acceptable solution be to $(this).removeClass("fu") at the end of my change function?