Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#14441 closed bug (notabug)

jQuery .data method should return always fresh values

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

Description

Hi!

As stated in the manual, the .data() method pulls in the HTML5 data- attributes only once per element.

However, this is bad from a debugging perspective - I would like to be able to do a $(el).attr("data-foo","bar") followed by either .data() or further .attr("data-foo","...") calls so that the current value of the data attribute is visible in the DOM inspector.

A quick testcase is at http://jsfiddle.net/ZYz4A/. Alternatively, I'd suggest placing something in the manual at http://api.jquery.com/data/ which says how one can quickly see the data attribute values in the Inspectors of Chrome, IE and Firefox.

Change History (6)

comment:1 Changed 6 years ago by marco@…

Furthermore, data() and the data selector are not compatible with each other.

The behaviour described in http://visualstudiomagazine.com/articles/2013/02/01/working-with-html5.aspx does not work when mixing .attr() and .data() writes to the data storage; I updated the fiddle to http://jsfiddle.net/ZYz4A/1/.

comment:2 Changed 6 years ago by dmethvin

#14443 is a duplicate of this ticket.

comment:3 Changed 6 years ago by dmethvin

#14442 is a duplicate of this ticket.

comment:4 Changed 6 years ago by dmethvin

Resolution: notabug
Status: newclosed

The properties of .data() are intentionally not serialized to the element on its dataSet, for example they could be a function or complex constructed objects. We are not going to change the documented semantics of .data().

comment:5 Changed 6 years ago by marco@…

Hi!

Thanks for the answer (and I'm fine with not changing the semantics due to BC)... but how is one then supposed to filter by the data attribute? Not serializing it back definitely breaks the [data-key=value] selector, so I'm re-opening the issue if you don't mind.

comment:6 Changed 6 years ago by dmethvin

Use .attr("data-xxxx") to set the data attribute, as described in the docs. There is no performant way to search the entire DOM for values set by .data() because it's not intended for those uses; use a class instead.

Note: See TracTickets for help on using tickets.