Bug Tracker

Modify

Ticket #7246 (closed bug)

Opened 3 years ago

Last modified 15 months ago

$('no elements').replaceWith('<div/>').length !=0

Reported by: jablko Owned by: jablko
Priority: low Milestone: 1.5
Component: manipulation Version: 1.4.3
Keywords: Cc:
Blocking: Blocked by:

Description

When .replaceWith() is called on a jQuery which didn't match any elements, it doesn't return the original jQuery object, it returns the new element that would have replaced it

This is the opposite behavior of when .replaceWith() is called on a jQuery which did match any elements

Change History

comment:1 Changed 3 years ago by snover

  • Owner set to jablko
  • Status changed from new to pending

Please provide a reduced test case.

comment:2 Changed 3 years ago by jitter

I guess this  test case shows what jablko is reporting. There are two buttons which show the different behavior when there are matched elements in the jQuery object and when there are none.

If the behavior of jQuery, when the "replaceWith no elements matched"-button in the test-case is clicked, is expected is a little unclear.

From the replaceWith-api

it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

Judging from this the previously empty jQuery document should be returned to the chain.

But there is also

In jQuery 1.4 .replaceWith() [...] can also work on disconnected DOM nodes. For example, with the following code:

$("<div/>").replaceWith("<p></p>");

The .replaceWith() method returns a jQuery set containing only a paragraph.

Here it seems jQuery internally regards an jQuery object which holds no matched elements and one which holds only disconnected DOM nodes as the same thus returning the newConent instead of the previously empty jQuery object.

If this is expected is left open to debate.

I personally feel it is the right thing to do as otherwise the replaceWith( newContent ) call would remain without effect which probably isn't the intention.

comment:3 Changed 3 years ago by trac-o-bot

  • Status changed from pending to closed

Automatically closed due to 14 days of inactivity.

comment:4 Changed 2 years ago by rwaldron

  • Priority changed from undecided to low
  • Component changed from unfiled to manipulation

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.