Skip to main content

Bug Tracker

Side navigation

#4346 closed bug (worksforme)

Opened March 13, 2009 10:17AM UTC

Closed December 09, 2009 09:33PM UTC

Data missing after adding to two elements.

Reported by: kondziu Owned by:
Priority: major Milestone: 1.4
Component: data Version: 1.4a1
Keywords: data, missing, multi element add Cc:
Blocked by: Blocking:
Description

jQ 1.2.6 and 1.3.2

jQuery('body').append(jQuery('<ul id="list1">'));

jQuery('body').append(jQuery('<ul id="list2">'));

creating element with data

var li = jQuery('<li>').text('x').data('foo', 'bar');

console.info(jQuery.data(li[0])); -> 2

console.info(jQuery.data(li[0], 'foo')); -> bar

after adding to two elements ...

jQuery('#list1, #list2').prepend(li);

console.info(jQuery.data(jQuery('#list1 li')[0])); -> 7 (OK)

console.info(jQuery.data(, 'foo')); -> undefined

console.info(jQuery.data(jQuery('#list2 li')[0])); -> 8 (OK)

console.info(jQuery.data(jQuery('#list2 li')[0], 'foo')); -> undefined

data is missing

Attachments (0)
Change History (5)

Changed March 13, 2009 10:21AM UTC by kondziu comment:1

//jQ 1.2.6 amd 1.3.2

jQuery('body').append(jQuery('<ul id="list1">'));
jQuery('body').append(jQuery('<ul id="list2">'));

//creating element with data
var li = jQuery('<li>').text('x').data('foo', 'bar');

console.info(jQuery.data(li[0]));  //-> 2
console.info(jQuery.data(li[0], 'foo'));  //-> bar

//after adding to two elements
jQuery('#list1, #list2').prepend(li);


console.info(jQuery.data(jQuery('#list1 li')[0])); //-> 7 (OK)
console.info(jQuery.data(jQuery('#list1 li')[0], 'foo')); // -> undefined


console.info(jQuery.data(jQuery('#list2 li')[0])); //-> 8 (OK)
console.info(jQuery.data(jQuery('#list2 li')[0], 'foo')); // -> undefined 


//data is missing

Changed March 14, 2009 12:55AM UTC by dmethvin comment:2

jQuery('#list1, #list2').prepend(li);

Since there are two elements in the jQuery object, the prepend is cloning the original li and appending the clone to the second element. The clone does not have the data.

I think the logic behind this is that copying all the

.data
to all elements would be expensive (and remember that
.data
includes all events as well). You still have the option to explicitly copy the data, or better still attach events and data after the implicit cloning occurs.

The docs aren't clear on this, so I'd suggest clearly documenting the behavior to resolve the ticket.

Changed June 02, 2009 10:16AM UTC by mikecapp comment:3

Same issue as #4191, I think. #4284 raises the same issue as an enhancement request.

Whether or not clone (explicit or otherwise) should copy data is a fair question, but it's hard to justify the first element generated by prepend etc getting data and the others not.

Changed August 09, 2009 01:38AM UTC by dmethvin comment:4

component: unfilleddata
milestone: 1.3.21.3.3

Changed December 09, 2009 09:33PM UTC by john comment:5

resolution: → worksforme
status: newclosed
version: 1.2.61.4a1

In this case you should clone the elements so that you explicitly carry over the data from the old ones (considering that what you describe doesn't carry over events either, this seems like a fair request).

Since #4191 is already fixed this should be as simple as doing .clone(true).