I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live()
stops working.
I get the error TypeError: $(...).live is not a function
.
Is there any method I can use in place of .live()
?
I recently updated jQuery from 1.8 to 2.1. I suddenly discovered that the .live()
stops working.
I get the error TypeError: $(...).live is not a function
.
Is there any method I can use in place of .live()
?
.live was removed in 1.9, please see the upgrade guide: http://jquery.com/upgrade-guide/1.9/#live-removed
The jQuery API documentation lists
live()
as deprecated as of version 1.7 and removed as of version 1.9: link.Furthermore it states:
jQuery
.live()
has been removed in version 1.9 onwards.That means if you are upgrading from version 1.8 and earlier, you will notice things breaking if you do not follow the migration guide below. You must not simply replace
.live()
with.on()
!Read before you start doing a search and replace:
For quick/hot fixes on a live site, do not just replace the keyword
live
withon
,as the parameters are different!
should map to:
The (child) selector is very important! If you do not need to use this for any reason, set it to
null
.Migration Example 1:
before:
after, you move the child element (
a
) to the.on()
selector:Migration Example 2:
before:
after, you move the element (
.myButton
) to the.on()
selector, and find the nearest parent element (preferably with an ID):If you do not know what to put as the parent,
body
always works:See also:
.live() was deprecated and has now been removed from jQuery 1.9 You should use .on()
A very simple fix that doesn't need to change your code, just add jquery migration script, download here https://github.com/jquery/jquery-migrate/
It supplies jquery deprecated but needed functions like "live", "browser" etc
If you happen to be using the Ruby on Rails' jQuery gem
jquery-rails
and for some reason you can't refactor your legacy code, the last version that still supports is2.1.3
and you can lock it by using the following syntax on yourGemfile
:then you can use the following command to update:
I hope that help others facing a similar issue.