Skip to main content

Bug Tracker

Side navigation

#14181 closed feature (plugin)

Opened July 24, 2013 07:56PM UTC

Closed July 24, 2013 09:15PM UTC

Last modified March 07, 2014 02:38PM UTC

Provide function similar to replaceWith which uses detach rather than remove

Reported by: bart@tremby.net Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:
Description

At present replaceWith() always cleans the removed nodes of handlers and data. It would be useful to be able to pass the "keep data" argument remove() accepts, or to have a new function which achieves the same end, similar to detach().

Attachments (0)
Change History (9)

Changed July 24, 2013 09:15PM UTC by rwaldron comment:1

resolution: → plugin
status: newclosed

Seems like a good plugin

(function(jQuery) {
  
  jQuery.fn.swapOut = function() {
    var args = jQuery.map( this, function( elem ) {
        return [ elem.nextSibling, elem.parentNode ];
      }),
      i = 0;

    // Make the changes, replacing each context element with the new content
    this.domManip( arguments, function( elem ) {
      var next = args[ i++ ],
        parent = args[ i++ ];

      if ( parent ) {
        // Don't use the snapshot next if it has moved (#13810)
        if ( next && next.parentNode !== parent ) {
          next = this.nextSibling;
        }
        jQuery( this ).detach();
        parent.insertBefore( elem, next );
      }
    // Allow new content to include elements from the context set
    }, true );

    // Force removal if there was no new content (e.g., from empty arguments)
    return i ? this : this.detach();
  };
}(jQuery))

(Untested, but probably works the way you want it to)

Changed July 24, 2013 10:28PM UTC by bart@tremby.net comment:2

Yes, and that's in fact almost exactly the implementation I came up with (it's just the replaceWith() function from the Jquery source but with remove() swapped out for detach()) but it would mean no duplication of code if it were in mainline Jquery, and it's certainly not an obscure request. On the contrary, it's a very-easily-filled gap in functionality. All it'd need is an extra argument to replaceWith() which is passed through verbatim to remove(), and then this could be either documented or undocumented with a separate documented method (just like detach() is a thin wrapper for remove()) which uses this extra argument.

Changed July 24, 2013 11:24PM UTC by rwaldron comment:3

Replying to [comment:2 bart@…]:

Yes, and that's in fact almost exactly the implementation I came up with (it's just the replaceWith() function from the Jquery source but with remove() swapped out for detach())

Yep ;)

but it would mean no duplication of code if it were in mainline Jquery,

It would be a duplication of code in jQuery core library... how would you overload replaceWith? It's a var arg function http://jsfiddle.net/rwaldron/UJQVB/

and it's certainly not an obscure request.

It's not? I've never read encountered this issue or seen it filed here... That's not to say it hasn't been, but I don't really want to search through 5 years of closed tickets.

On the contrary, it's a very-easily-filled gap in functionality. All it'd need is an extra argument to replaceWith() which is passed through verbatim to remove(),

And there it is... again, see: http://jsfiddle.net/rwaldron/UJQVB/

Changed July 24, 2013 11:54PM UTC by bart@tremby.net comment:4

Ah, true. I guess it would be possible as an optional first boolean argument. I guess that's complicating things.

Changed July 24, 2013 11:59PM UTC by rwaldron comment:5

That's a mistake that we'll never make again ;)

Changed July 25, 2013 12:01AM UTC by bart@tremby.net comment:6

Complicating things? Or an optional first boolean? Pretty simple code, just wouldn't be the most intuitive. But again, it could be undocumented and used only internally as with the argument to remove().

Changed July 25, 2013 12:04AM UTC by rwaldron comment:7

Or an optional first boolean?

That is the mistake we'll never make again (the original mistake is in jQuery.extend)

But again, it could be undocumented and used only internally as with the argument to remove().

But you're asking for this api as a public api for you to use...?

Can we move on?

Changed July 25, 2013 12:15AM UTC by bart@tremby.net comment:8

> But again, it could be undocumented and used only internally as with the argument to remove(). But you're asking for this api as a public api for you to use...?

I meant for a separate new documented function to use. Just like (as I've mentioned before) remove() accepts an argument which is undocumented, but which the documented detach() function uses.

Can we move on?

By all means use your time however you like.

Changed March 07, 2014 02:38PM UTC by dmethvin comment:9

#13400 is a duplicate of this ticket.