This one is slightly better altough not in jQuery style: $.fn.replaceWithAndReturnNew = function(html){ var elm = $(this)[0].cloneNode(false); $(this).after(elm).remove(); elm.innerHTML = html; return $(elm);};
On Sep 20, 9: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