Skip to main content

Bug Tracker

Side navigation

#7246 closed bug ()

Opened October 19, 2010 12:32AM UTC

Closed November 11, 2010 11:09PM UTC

Last modified March 13, 2012 08:33PM UTC

$('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

Attachments (0)
Change History (4)

Changed October 19, 2010 12:57AM UTC by snover comment:1

owner: → jablko
status: newpending

Please provide a reduced test case.

Changed October 19, 2010 08:10AM UTC by jitter comment:2

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.

Changed November 11, 2010 11:09PM UTC by trac-o-bot comment:3

status: pendingclosed

Automatically closed due to 14 days of inactivity.

Changed January 31, 2011 04:56PM UTC by rwaldron comment:4

component: unfiledmanipulation
priority: undecidedlow