How to change my code from .live () to .on ()

I have a live() function in my jquery below:

 $("#qandatbl td.weight input").live("change", calculateTotal); function calculateTotal() { var totalweight = hundred; $("#qandatbl td.weight input").each(function (i, elm){ totalweight = totalweight - parseInt($(elm).val(), 10); }); $("#total-weight").text(totalweight).append('%').css('font-weight', 'bold'); } 

Now some say that the live() function is slowing down and it is better to use the on() function. If so, then how do I change the above code to on() and not to the live() function? Is it important that I do not use live() or is it not a big deal?

+7
source share
1 answer

According to the documentation :

Rewriting the .live () method in terms of its successors is simple; these are templates for equivalent calls for all three ways of nesting events:

 $(selector).live(events, data, handler); // jQuery 1.3+ $(document).delegate(selector, events, data, handler); // jQuery 1.4.3+ $(document).on(events, selector, data, handler); // jQuery 1.7+ 

So, in your case, if you are using jQuery 1.7+, it will be:

 $(document).on('change', '#qandatbl td.weight input', calculateTotal); 
+9
source

All Articles