Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#14181 closed feature (plugin)

Provide function similar to replaceWith which uses detach rather than remove

Reported by: bart@… 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().

Change History (9)

comment:1 Changed 10 years ago by Rick Waldron

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)

comment:2 Changed 10 years ago by 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()) 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.

comment:3 in reply to:  2 Changed 10 years ago by Rick Waldron

Replying to 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/

comment:4 Changed 10 years ago by bart@…

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

comment:5 Changed 10 years ago by Rick Waldron

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

comment:6 Changed 10 years ago by bart@…

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().

comment:7 Changed 10 years ago by Rick Waldron

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?

comment:8 in reply to:  7 Changed 10 years ago by bart@…

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.

comment:9 Changed 9 years ago by dmethvin

#13400 is a duplicate of this ticket.

Note: See TracTickets for help on using tickets.