Live events with on() instead of live()?

Live events with on() instead of live()?

ulrikeulrike Posts: 39Questions: 1Answers: 0
edited November 2012 in General
Hello,

I have been wanting to use live events as explained here : http://datatables.net/release-datatables/examples/advanced_init/events_live.html
This actually works well, using the live() function.
However, jQuery recommends now using on() instead of live(). See http://api.jquery.com/live/
I tried replacing the live() function by on() and my code stopped working.

I just wanted to know if you will support on() and if the problem is even linked to datatables itself.
I am using jquery 1.8.1 and datatables 1.9.3

Best regards.

Replies

  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    edited November 2012
    Working modification of the 'live' example: http://live.datatables.net/itotew/edit#javascript,html

    I'll be updating all of the documentation on the DataTables site as part of the work on 1.10 to use live, but in general, it has nothing to do with DataTables, since you are just using a jQuery event handler.

    Allan
  • codemonkcodemonk Posts: 24Questions: 0Answers: 0
    edited November 2012
    I also am using the .live() when i tried using .on() it didn't work. Here is my code to select a given row. I then go about retrieving all the data from that row.

    [code]
    //Clickable Rows
    $("#example tbody tr").live("click", function() {
    //Disable the edit button
    if($(this).hasClass('row_selected')){
    $(this).removeClass('row_selected');
    $("#btn-row-edit").button({disabled: true});
    }
    //Enable the edit button
    else {
    oTable.$('tr.row_selected').removeClass('row_selected');
    $(this).addClass('row_selected');
    $("#btn-row-edit").button({disabled: false});
    }
    });
    [/code]

    I'll look into changing it based off allan's suggestion. I cannot remember why i didn't change over to .on() i think it may have been because i could not use the $(this) function.
  • allanallan Posts: 63,532Questions: 1Answers: 10,475 Site admin
    [code]
    $("#example tbody").on("click", 'tr', function() {
    [/code]

    should work just fine.

    Allan
  • ulrikeulrike Posts: 39Questions: 1Answers: 0
    Works indeed!

    I can't get everything to work correctly using on() with FixedColumns though. However, this is a just a sidenote which I can't get more into detail right now.
This discussion has been closed.