Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13130 closed bug (notabug)

data attributes not updated after appendTo

Reported by: astoute@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: git
Keywords: Cc:
Blocked by: Blocking:

Description

Example: If you have a div with a data-mykey and you move this div using appendTo and then change the data attribute, there is unexpected behaviour.

Query the element directly and it reports the new value for the data attribute. As Expected.

Search the document for items with the new value for the data attribute and you will find none. Expected: you should find the div.

Search the document for items with the old value for the data attribute and you will find it. Expected: you should not find this element by its old attribute value.

JsFiddle: http://jsfiddle.net/analogNerd/3N5zA/1/

Change History (7)

comment:1 Changed 7 years ago by dmethvin

Resolution: notabug
Status: newclosed

That is correct behavior and documented. The .data() method only reads data attributes once, and never sets data attributes. Use .attr() if you are trying to set data attributes rather than the internal .data() cache.

comment:2 Changed 7 years ago by Rick Waldron

#13440 is a duplicate of this ticket.

comment:3 Changed 7 years ago by gibson042

#13482 is a duplicate of this ticket.

comment:4 Changed 7 years ago by anonymous

This is a bug. Not sure how you can consider it not a bug.

comment:5 Changed 7 years ago by Rick Waldron

Not a bug: it's a specific design choice for the sake of performance and compatibility with jQuery's original jQuery.fn.data() which predates HTML5 custom data-* attrs. If you need to set custom data-* attrs, use the attr() method.

comment:6 Changed 7 years ago by anonymous

Feel free to update the documentation to make mention of this bug then. The documentation and the way it functions are not consistent.

comment:7 Changed 7 years ago by Rick Waldron

Feel free to READ the documentation: http://api.jquery.com/data/#data-html5

The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

Note: See TracTickets for help on using tickets.