Richard, Thanks for your reply. It is neat but it leaks memory on IE. While the following not:
$.fn.replaceWithAndReturnNew = function(html){ var div = document.createElement('div'); var replaced = this[0]; replaced.parentNode.replaceChild(div, replaced); div.innerHTML = html; var replacer = div.firstChild; div.parentNode.replaceChild(replacer, div); return $(replacer);}; The trick here is to insert the HTML after placing the element in the DOM. I've set a 1000 time loop over the example 4 at the page http://beebole.com/pure/getting_started.html#example4 And with IE it's clear the memory is climbing fast. Not sure this can be a bug for jQuery, as we are a bit extreme here on the innerHTML usage. Cheers, On Sep 20, 2:31 pm, "Richard D. Worth" <[EMAIL PROTECTED]> wrote: > $.fn.replaceWithAndReturnNew = function(htmls){ > var replacer = $(htmls); > $(this).replaceWith(replacer); > return replacer; > > }; > > - Richard > > On Sat, Sep 20, 2008 at 3:31 AM, tchvil <[EMAIL PROTECTED]> wrote: > > > Thanks a lot for your replies. > > Ok, I guess there are good reasons to return the replaced object. > > > Now my question becomes... is there a better jQuery way of doing this > > quite ugly thing: > > > $.fn.replaceWithAndReturnNew = function(htmls){ > > var replaced = $(this)[0]; > > var div = document.createElement('DIV'); > > div.innerHTML = htmls; > > var replacer = div.firstChild; > > replaced.parentNode.replaceChild(replacer, replaced); > > return $(replacer);}; > > > There's a working page there:http://friendpaste.com/GkTTkwQV > > > Cheers, > > > On Sep 18, 1:09 am, Ariel Flesler <[EMAIL PROTECTED]> wrote: > > > Sorry, I missunderstood the initial (and subsequential) posts. > > > The present behavior (return the initially matched element) is the > > > correct one. That's what I'd expect and it matches the behavior of > > > remove(), append(), etc. > > > > Don't file any bug report :) > > > > Thanks Richard for pinging me about this. > > > > -- > > > Ariel Fleslerhttp://flesler.blogspot.com/ > > > > On Sep 17, 6:12 pm, Ariel Flesler <[EMAIL PROTECTED]> wrote: > > > > > I think this is a bug, I'd expectreplaceWithto return the old set, > > > > just like append/etc. > > > > > Can you (tchvil) file a bug with a test case ? put it under core, > > > > assigned to me. > > > > > Thanks > > > > -- > > > > Ariel Fleslerhttp://flesler.blogspot.com/ > > > > > On Sep 17, 8:04 am, "Richard D. Worth" <[EMAIL PROTECTED]> wrote: > > > > > > On Wed, Sep 17, 2008 at 6:58 AM, Karl Rudd <[EMAIL PROTECTED]> > > wrote: > > > > > > > Nah, it's not a bug. > > > > > > >replaceWith( content ) Returns: jQuery > > > > > > Replaces all matched elements with the specified HTML or DOM > > elements. > > > > > > This returns the JQuery element that was just replaced, which has > > been > > > > > > removed from the DOM > > > > > > > (http://docs.jquery.com/Manipulation) > > > > > > It may be well documented, but that doesn't mean it can't be a bug. > > ;) > > > > > > > It's like doing a remove(), followed by an append(). Like > > "remove()" > > > > > > it also "returns" (well keeps in the jQuery collection) the > > elements > > > > > > it removes. > > > > > > This is quite a fundamental difference. Since .remove() returns a DOM > > > > > Element, it's not chainable. SincereplaceWith() returns a chainable > > jQuery > > > > > object, it's reasonable to expect methods you call on that chain to > > effect > > > > > the new element, not your now removed/disconnected element(s). Like > > many > > > > > other methods, it could modify the chain. And if you wanted to get > > back to > > > > > the previous chain, you could call .end(). My 2c. > > > > > > - Richard