carlg carlg - 1 year ago 217
Javascript Question

Jquery .on with doubleclick event

Why would this work :

$(document).on("dblclick", "#areaA tr:has(td)", function(e) {
//code here

and this does not

$("#areaA tr:has(td)").on('dblclick', function(e) {
//Code here

I'm following the example on the jquery documentation page exactly, but my double click does not fire. When I do it the first way, it works, but seems like it fires the event twice.

This is in the context of a Kendo UI grid.

Is there really a difference between these two pieces of code?

Answer Source

The main difference is that the condition in the first one will be checked each time you click. So if the element with id areaA or the tr or td inside is added dynamically, only the first one can work.