views:

100

answers:

6

Here's my code:

HTML:

<ul id="more-items">
   <li><label class="button">hi</label></li>
   <li><label class="button">hi</label></li>
   <li><label class="button">hi</label></li>
</ul>

Javascript:

$('ul#more-items label.button').live('click', function()
{
   alert(1);
});

Clicking on the labels doesn't cause the alert() to fire. The .live() bind seems to have no affect on the label element...

If I change it to use .click() or even .bind(), it works perfectly. Is there any reason why .live() isn't working? Is there a way to fix this?

The reason I'm using .live() is because ul#more-items will be readded to the page multiple times and I don't want to have to fire off a function each time that happens to rebind the click.

A: 

try

$('label.button').live('click', function()
{
   alert(1);
});

or

$('ul#more-items').delegate('label.button','click', function()
{
   alert(1);
});
Reigel
Just tried both and neither worked
Steffan
A: 

It seems ok, but your alert should be alert("1");

or:

var 1="1";

alert(1);

Zuul
hmmm.... really?? serious???
Reigel
alert(1) is valid. to test type javascript:alert(1) in your address bar. you will see that it properly alerts the number 1.
John Hartsock
@Reigel, could I be wrong ? :( [ @John Hartsock, humm... tks for the head's up :) ]
Zuul
yes!.. sorry Zuul, but `alert(1);` is good...
Reigel
@Reigel, ... I should be sleeping right now :) lol
Zuul
@Zuul - Well, I'm guessing you should.. ;) don't overuse the brain... ;) sleep well.. ;)
Reigel
@Reigel, It's 4AM here, yesterday was an holly-day, but I've been working since 10AM, that makes 18hours of codding on a supposed quiet day :) (well... gonna sleep) ps: catch you tomorrow ;)
Zuul
+1  A: 

Element ids must be unique. Your stament "The reason I'm using .live() is because ul#more-items will be readded to the page multiple times.. " tells me that you will have multiple of these:

<ul id="more-items"> 
  <!--Stuff-->
</ul>
<ul id="more-items"> 
  <!--Stuff-->
</ul>

This would be invalid HTML as the ID more-items exists twice.

John Hartsock
Forgot I was readding that UL multiple times when I was writing the HTML >.< Changed it to class now. Doesn't fix the .live() problem, though.
Steffan
Did you change the selector too? `ul.more-items label.button` (change "#" to ".")
Pointy
A: 

Your code works for me. What version of jquery are you using? Are you defining the javascript in a $(document).ready function?

zaius
Yeah, I've got my code in the $(document).ready function. I'm using: http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
Steffan
Here's your code in an empty page, and it works: http://david.kelso.id.au/live.html - guessing there's some other problem. Possibly malformed markup?
zaius
+4  A: 

jQuery's live() method relies on event bubbling (all the way up to the root node) to work.

If there's any code that will stop the bubbling of the event on either the label or any of its ancestors, live() will not work.

Any chance that the label or one of its ancestors has a handler with either of the following?

return false;

or

event.stopPropagation();

If so, live() will fail.


Relevant jQuery docs:

patrick dw
I just stumbled upon this problem. My labels have a plugin binded to them that returns false. The reason for this is the .click() function (binded to the label) fires twice unless false is returned. See: http://groups.google.com/group/jquery-en/browse_thread/thread/af789262532a4c68
Steffan
That will do it. The `return false;` causes the bubbling to halt, so the handler, which is at the root of the DOM tree, never gets notified to fire. I assume the elements are added dynamically. If so, you should be able to use `bind` instead of `live` when you create the elements. Then there's no issue.
patrick dw
A: 

You can try this. It may work

Change:

$('ul#more-items label.button').live('click', function() 
{ 
   alert(1); 
}); 

To This:

$('ul#more-items label.button').unbind('click').live('click', function() 
{ 
   alert(1); 
}); 
Luke101