Asked  6 Months ago    Answers:  5   Viewed   43 times

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()?

 Answers

75

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 with on,
as the parameters are different!

.live(events, function)

should map to:

.on(eventType, selector, function)

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:

$('#mainmenu a').live('click', function)

after, you move the child element (a) to the .on() selector:

$('#mainmenu').on('click', 'a', function)

Migration Example 2:

before:

$('.myButton').live('click', function)

after, you move the element (.myButton) to the .on() selector, and find the nearest parent element (preferably with an ID):

$('#parentElement').on('click', '.myButton', function)

If you do not know what to put as the parent, body always works:

$('body').on('click', '.myButton', function)

See also:

  • jQuery - how to use the “on()” method instead of “live()”?
  • jQuery 1.9 Migration Guide
Tuesday, June 1, 2021
 
pocketfullofcheese
answered 6 Months ago
68

Updated answer

getJSON returns a promise (a read-only deferred), so you can listen to it. But since you need some post-processing, you'd want to chain a then which allows you to alter the resolved value.

// Now using `then`
function getThumbnail(vUrl){
  return $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl).then(function(data){
    return {
      thumbnail:data.thumbnail_url,
      vurl:vurl
    }
  });
}

//and in your call will listen for the custom deferred's done
getThumbnail('the_vurl_').then(function(returndata){
  //received data!
});

Original answer

You can use a deferred object, and listen for the done().

function getThumbnail(vUrl) {
    //create our deferred object
    var def = $.Deferred();

    //get our JSON and listen for done
    $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl)
        .done(function(data){

            //resolve the deferred, passing it our custom data
            def.resolve({
                thumbnail:data.thumbnail_url,
                vurl:vurl
            });
        });

    //return the deferred for listening
    return def;
}

//and in your call will listen for the custom deferred's done
getThumbnail('the_vurl_')
    .done(function(returndata){
        //received data!
    });

You could return $.getJSON's deferred to get the raw data. But because of "post-processing" into an object, the custom deferred is needed. You could also pass a callback to getThumbnail():

function getThumbnail(vUrl,callback) {
    $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl,function(returndata){
        callback(returndata);
    });
}

getThumbnail('the_vurl_',function(returndata){
    //received data!
})
Wednesday, July 28, 2021
 
matthy
answered 4 Months ago
47

The problem is that element is a DOM node, without access to jQuery methods, what you need to use is $(this) or $(element):

elements.each(function(index, element) {
    $(this).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

Or:

elements.each(function(index, element) {
    $(element).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

Or you could, instead, use cssText (if you really want to work with DOM nodes):

elements.each(function(index, element) {
    element.style.cssText = 'width: ' + elemWidth + '; height: ' + elemHeight + ' top: ' + elemTop + '; left: ' + elemLeft;
}

Bear in mind that, within each(function (index, element){ ... }); element is exactly the same as this (and, as a corollary, $(this) is the same as $(element)). I'm not sure quite what the benefit is, of using the element at all.

Tuesday, August 17, 2021
 
subroutines
answered 4 Months ago
99

The imports have to be in certain order:

jquery.min.js

jquery-ui.min.js

jquery.dataTables.min.js

jquery.jeditable.js

jquery.dataTables.editable.js

jquery.validate.js

After fixing this, update to latest versions fixed the problem!

Wednesday, August 18, 2021
 
muffe
answered 4 Months ago
62

You're making your request via AJAX, which by definition is asynchronous. That means you're returning from the function before the AJAX request completes. In fact, your return statement is meaningless as it returns from the callback function, not your addToPlaylist function.

You have a couple of choices. The first one is better.

First, you can work with the asynchronous nature of the AJAX request and pass a callback into your addToPlaylist method (much like you're passing in the anonymous callback to the ajax function) and have the AJAX callback, call that function instead of doing the return. That way your request completes asynchronously and doesn't lock up your browser while it's going on.

function addToPlaylist(component_type, add_to_pl_value, pl_list_no, cb )
{
    ...yada yada yada...
    $.ajax({
         ...
         success: function(data) {
            ...
            if (cb) {
               cb.apply(this, rendered_item );
            }
         }
    });
}

Second, you can add the option aSync: false to the ajax call. This will force the AJAX call to run synchronously (essentially it just loops until the call returns then calls your callback). If you do that, you need to capture a local variable in your addToPlaylist function inside the callback and assign the value(s) to it from the callback. At the end of the addToPlaylist function, return this variable as the result.

function addToPlaylist(component_type, add_to_pl_value, pl_list_no )
{
    ...yada yada yada...
    var result = null;
    $.ajax({
         aSync: false,
         ...
         success: function(data) {
            ...
            result = rendered_item;
         }
    });

    return rendered_item;
}
Saturday, August 21, 2021
 
Thomas Weller
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share