Ticket #6439 (closed bug: fixed)
append() removes all data when used on multiple elements
| Reported by: | thesaint | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | |
| Component: | manipulation | Version: | 1.4.2 |
| Keywords: | append data events | Cc: | |
| Blocking: | Blocked by: |
Description
When a fragment with data is created and it is appended with "append()" to multiple elements, the fragment is inserted without the data (thus, event handlers are also wiped). "appendTo()" doesn't show this behavior. Also, when "append()" is used for a single element, the data isn't lost. See the attached file.
Attachments
Change History
comment:1 Changed 3 years ago by dmethvin
I can see how this would happen, since the elements have to be cloned to append to multiple locations. Were you expecting to see the data cloned into new separate objects that would be associated with the elements? Or would you expect the elements to all share the original common data object?
comment:2 Changed 3 years ago by thesaint
Hmm, firstly I expected the data to be there at all :-)
Here are my thoughts: The most important use case for data are bound events. From a memory perspective it seems better to me when the event handler is shared across all elements. And at the moment I can't think of a scenario where cloned event handlers are more useful than shared event handlers. What happens when you call .bind() on multiple elements, is the handler cloned? What's the behavior of appendTo()? I think consistency is a good thing here.
comment:5 Changed 3 years ago by dmethvin
I guess this is a case of two cases that look similar justifiably doing very different things.
For the case $("#elem1").appendTo("#elem2") I think we *want* to have everything preserved; we are just moving #elem1 to another point in the DOM tree. For example you might move an option element between two select boxes.
For $("#elem1").appendTo(".lotsaElems") I am less confident. (For one, if it were truly an element with an id you've created an invalid document but let's ignore that for a second.) I can see a strong case for cloning the attached data (and thus the events as well).
comment:6 Changed 2 years ago by danheberden
Seems to be working properly - http://jsfiddle.net/danheberden/EKB4x/
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

