Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#7246 closed bug

$('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:
Blocked by: Blocking:

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

comment:1 Changed 12 years ago by snover

Owner: set to jablko
Status: newpending

Please provide a reduced test case.

comment:2 Changed 12 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 12 years ago by trac-o-bot

Status: pendingclosed

Automatically closed due to 14 days of inactivity.

comment:4 Changed 12 years ago by Rick Waldron

Component: unfiledmanipulation
Priority: undecidedlow
Note: See TracTickets for help on using tickets.