Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Converting .live to .on click [duplicate]

Possible Duplicate:
jQuery 1.7 - Turning live() into on()
.live() vs .on() method

I have the following code (which I've simplified for this) which worked fine alongside jquery-1.7.

$("td.ui-datepicker-week-col a").live("click", function () {
    alert("hello");
});

I'm trying to upgrade my code though so I can upgrade my jQuery pack version so I've changed it to this.

$("td.ui-datepicker-week-col").on("click", "a", function () {
    alert("hello");
});

This doesn't do anything though. It doesn't throw any errors either so I can't see where the problem is.

Any ideas?

like image 203
Tom Avatar asked Dec 09 '22 18:12

Tom


1 Answers

If you don't have "td.ui-datepicker-week-col" element at start, then you must choose an existing permanent set. For example

$(document).on("click", "td.ui-datepicker-week-col a", function () {
    alert("hello");
});

The on function, contrary to live, will only work with the set as it is defined when you call the binding function. What is dynamic is the interpretation of the selector passed as argument to on when an event occurs.

like image 158
Denys Séguret Avatar answered Dec 11 '22 07:12

Denys Séguret