Search code examples
javascriptlivejquery

Why jQuery live() does not work?


I have the following code which work fine:

$('.ui-selectmenu-menu a').click(function() { alert('OK'); });

However, if I replace it with:

$('.ui-selectmenu-menu a').live('click', function() { alert('OK'); });

it does not work.

What could be the reason for that ?

(In my case, $('.ui-selectmenu-menu a') elements could be removed and added again during the run.)


Solution

  • If the class changes e.g. the parent doesn't have class="ui-selectmenu-menu then the selector would no longer match, make sure this isn't happening after whatever events you have.

    Unlike binding directly to the element, the selector no longer matching will stop a .live() handler from firing for that element's events.