tags:

views:

49

answers:

3

I am using jquery as so

$('.mybutton').live('click', function(){
   // do something
});

this is called when the document is ready but when the document is ready there are 'mybutton' classes being used, but if the user clicks somewhere a new form appears which uses the button with the class 'mybutton'. But this does not seem to be working, and it doesn't have the required handler.

Is this because there where no 'mybutton' classes to start with on the document ready?

A: 

EDIT: okay, i miunderstood your question.
This is more an answer to "Is a selector always required with live()?".


Yes.
See the caveats section on this wiki page.

The reason why a selector is required with jquery.live() is explained here.

Here Be Wolves
A: 

Perhaps you could try this instead:

$('#myForm').delegate('.mybutton', 'click', function() {
   // do something.
})
mkoistinen
+1  A: 

If live() ever doesn't seem to work for an element that has the proper selector, like .mybutton in your case, it is likely because some ancestor element of the .mybutton is preventing bubbling from happening.

If any ancestor of a .mybutton has:

return false;

or:

event.stopPropagation();

This will effectively disable .live() for that .mybutton because .live() needs the event to bubble all the way up to the root.

patrick dw
@Jas - Good catch. Thanks for correcting my spelling flub. :o)
patrick dw
@patrick - No prob. :-)
JasCav
This seemed to fix the problem, sorry for not accepting the answer earlier, internet problems see :)
wiggles
@wiggles - Not a problem. :o) Glad it worked out for you.
patrick dw