Re: [jQuery] Question about binding .click() (and other events) to parentless elements
This won't help your problem at all, but I would also like to add that I've found exceptions in IE6 when adding css attributes to an element that hasn't still been attached to the DOM, i.e: var elem = $(li).css(color, red); I don't know if these problems are still present in the new release 1.1.2. Abel. On 3/1/07, Weaver, Scott [EMAIL PROTECTED] wrote: I noticed somewhat odd behavior when using .click() on newly created elements. If the .click() is assigned prior to the element being attached to the DOM, the element behaves as if there is no event bound to it. var removeLink = $(a href='#'Remove/a).click(function() {alert('hello');}); $(.portlet .handle).append(removeLink); Tested this in both FF 1.5 and IE6 and the event appears to have never been bound to the element. However, this works fine: var removeLink = $(a href='#'Remove/a); $(.portlet .handle).append(removeLink); $(.portlet .handle a).click(function() {alert('hello');});; Is this expected behavior? It is not a big deal at all though it did cost me a bit of time earlier this morning try to figure why things weren't working. If this is expected behavior a short note/warning in event docs would be nice :) Regards, -scott ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] Question about binding .click() (and other events) to parentless elements
Quite expected behavior :-) You should always use jQuery( htmlSnippet ) and attach it to the dom as soon as possible. This also works fine, and is a typical chain: var removeLink = $( # Remove ) .appendTo( .portlet .handle ) .click( function() { alert( 'hello' ); } ); --Andrea Weaver, Scott-2 wrote: I noticed somewhat odd behavior when using .click() on newly created elements. If the .click() is assigned prior to the element being attached to the DOM, the element behaves as if there is no event bound to it. var removeLink = $( # Remove ).click(function() {alert('hello');}); $(.portlet .handle).append(removeLink); Tested this in both FF 1.5 and IE6 and the event appears to have never been bound to the element. However, this works fine: var removeLink = $( # Remove ); $(.portlet .handle).append(removeLink); $(.portlet .handle a).click(function() {alert('hello');});; Is this expected behavior? It is not a big deal at all though it did cost me a bit of time earlier this morning try to figure why things weren't working. If this is expected behavior a short note/warning in event docs would be nice :) Regards, -scott ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- View this message in context: http://www.nabble.com/Question-about-binding-.click%28%29-%28and-other-events%29-to-parentless-elements-tf3327728.html#a9253176 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/