views:

36

answers:

2

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.)

+1  A: 

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.

Nick Craver
In my case, the element `$(".ui-selectmenu-menu")` is removed and then created again (with its <a> childs). Is this stops the .live() on `$(".ui-selectmenu-menu a")` from working ? What workaround would you suggest in my case ?
Misha Moroshko
+1  A: 

Nick's answer makes sense. But also, check that you have jquery version 1.3 or greater. .live() was added in jquery version 1.3.

simplyharsh
I use jQuery 1.4.2
Misha Moroshko