0

The following code worked fine with jquery 1.7.2. But it doesn't with jquery 1.9.1. It gives an error with "Uncaught TypeError. Object [object Object] has no method 'live'.

How can I fix the problem?

enter image description here

<script>
$(document).ready(function(){

    var tablecont = $("#tablesorter1");
    var module = "category";

    function updateitem()
    {    
        $.ajax({
            type: "POST", 
            url: "http://localhost/tangen2014/index.php/category/admin/Ajaxgetupdate", 
            complete: function(data)
            {
                tablecont.html(data.responseText);
            }
        });
    }

        //on submit event
    $(".changestatus").live('click', function(event){
        event.preventDefault();
        var href = $(this).attr("href");
        var id =href.substring(href.lastIndexOf("/") + 1);
        $.ajax({
            type: "POST", 
            url: "http://localhost/tangen2014/index.php/kaimonokago/admin/changeStatus"+"/"+module+"/"+id,
            complete: function()
            {
                updateitem();
            }
        });  
    });
});
</script>
halfer
  • 19,824
  • 17
  • 99
  • 186
shin
  • 31,901
  • 69
  • 184
  • 271
  • possible duplicate of [jQuery live() works with jQuery 1.8.3 & below but not with 1.9.1 or 2.0](http://stackoverflow.com/questions/16543309/jquery-live-works-with-jquery-1-8-3-below-but-not-with-1-9-1-or-2-0) – Jacob Mar 25 '14 at 01:37

2 Answers2

2

From the upgrade guide

The .live() method has been deprecated since jQuery 1.7 and has been removed in 1.9. We recommend upgrading code to use the .on() method instead

So you need to use .on() here:

$('#tablesorter').on('click', ".changestatus", function(event){
    // Your code here
});

It's also better to delegate the events to the closest static parent instead of document level for better performance.

Felix
  • 37,892
  • 8
  • 43
  • 55
  • I don't think there's any significant *performance* reason to pick a close parent, but there are other good reasons. Bubbling is done in native browser code, so I doubt there's any measurable difference in bubbling all the way up to the document. – Pointy Mar 25 '14 at 01:43
1

The .live() method has been deprecated since like forever, and it was yanked completely in 1.9.

Use:

    $(document).on('click', ".changestatus", function(event){

It doesn't have to be $(document); it can be any handy container element. That is, any element that's a parent of your ".changestatus" elements.

The .live() method was a good idea implemented badly. For a while there was .delegate(), which worked more like the modern all-in-one .on() does with three parameters.

Pointy
  • 405,095
  • 59
  • 585
  • 614