You can also do the opposite: $('<div>my div</div>').replaceAll (someHtmlElement).somethingWithTheNewElement()
- ricardo On Dec 4, 5:02 pm, machineghost <[EMAIL PROTECTED]> wrote: > For some reason I didn't think that would work, because of issues with > $myDiv not being on the page when it's created (and events only being > hook-up-able to on-page elements). But after testing your example I > realized that jQuery does keep it's references straight even when you > create the element "off screen", so thanks; that actually solves my > problem perfectly! > > Man, I love it when I think "this can't be right", post to a forum, > and in less than two hours have a helpful response that explains "it's > not right; here's where you are misunderstanding". > > Jeremy > > On Dec 4, 10:38 am, "Michael Geary" <[EMAIL PROTECTED]> wrote: > > > Don't forget that you don't have to do everything with chaining. Simply grab > > a reference to any jQuery object or DOM element you want to use later. > > > var $myDiv = $('<div>my div</div>'); > > $(someHtmlElement).replaceWith($myDiv); > > $myDiv.click( function() { alert( $(this).html() ); } ); > > > -Mike > > > > From: machineghost > > > > While I love most things in jQuery, one thing I don't get is > > > replaceWith. This method returns the object you just > > > replaced, which as far as I can see is 100% worthless, rather > > > than the the object you're replacing it with. If you do: > > > > $.(someHtmlElement).replaceWith("<div>my div</div>"); > > > > There is no way whatsoever (as far as I can tell at least) to > > > access "<div>my div</div>" after the replacement. You can > > > access someHtmlElement, but that's not very helpful; if > > > you're replacing it, you probably don't care about it anymore.