Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13987 closed bug (notabug)

Appending jquery objects to multiple elements fails under certain conditions

Reported by: ctrl1alt1del Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.0.2
Keywords: Cc:
Blocked by: Blocking:

Description

Appending a Jquery object, which has been stored as a variable, to multiple elements fails to append the object to all of the matched elements. The jsfiddle below demonstrates an example of the behavior. It shows that only one of the elements selected with a class selector receives the appended object. This occurs when the same Jquery object variable is appended to another element later in the code block.


http://jsfiddle.net/ZEBKK/


Browsers affected:
Firefox 21.0 with Jquery 2.0.2
Google Chrome 27.0.1453.94 m with Jquery 2.0.2
Internet Explorer 9 with 2.0.2 on my local machine and Jquery 1.9.1 inside jsfiddle(2.0.2 doesn't run at all)

Change History (2)

comment:1 Changed 7 years ago by dmethvin

Resolution: notabug
Status: newclosed

Your second append is appending the original element. See the docs:

If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first. -- http://api.jquery.com/append/

Docs correction, it should say _except the last_ instead of _after the first_ there, but the behavior is expected. I've created https://github.com/jquery/api.jquery.com/issues/318 to correct that.

If you are going to create a template-ish element and want to always copy rather than move, use .clone().append() instead.

comment:2 Changed 7 years ago by ctrl1alt1del

Thanks for the clarification and docs correction.

Note: See TracTickets for help on using tickets.