My application has dynamically added Dropdowns. The user can add as many as they need to.
I was traditionally using jQuery's live()
method to detect when one of these Dropdowns was change()
ed:
$('select[name^="income_type_"]').live('change', function() {
alert($(this).val());
});
As of jQuery 1.7, I've updated this to:
$('select[name^="income_type_"]').on('change', function() {
alert($(this).val());
});
Looking at the Docs, that should be perfectly valid (right?) - but the event handler never fires. Of course, I've confirmed jQuery 1.7 is loaded and running, etc. There are no errors in the error log.
What am I doing wrong? Thanks!
Just found a better solution which doesn't involve editing third party code:
https://github.com/jquery/jquery-migrate/#readme
Install the jQuery Migrate NuGet package in Visual Studio to make all the versioning issues go away. Next time Microsoft update their unobtrusive AJAX and validation modules perhaps try it without the migrate script again to see if they resolved the issue.
As jQuery Migrate is maintained by the jQuery Foundation I think this is not only the best approach for third party libraries and also to get warning messages for your own libraries detailing how to update them.
In addition to the selected answer,
Port
jQuery.live
to jQuery 1.9+ while you wait for your application to migrate. Add this to your JavaScript file.Note: Above function will not work from jQuery v3 as
this.selector
is removed.Or, you can use https://github.com/jquery/jquery-migrate
The
on
documentation states (in bold ;)):Equivalent to
.live()
would be something likeAlthough it is better if you bind the event handler as close as possible to the elements, that is, to an element being closer in the hierarchy.
Update: While answering another question, I found out that this is also mentioned in the
.live
documentation:jquery verision x.x.x.js open editor, find
jQuery.fn.extend
add code
Example : jquery-2.1.1.js --> line 7469 (jQuery.fn.extend)
Example images view
In addition to the selected answers,
If you use Visual Studio, you can use the Regex Replace.
In Edit > Find and Replace > Replace in Files
Or Ctrl + Shift + H
In Find and Replace pop-up, set these fields
Find what:
\$\((.*)\)\.live\((.*),
Replace with:
$(document.body).on($2,$1,
In find options check "Use Regular Expressions"