Bug Tracker

Opened 10 years ago

Closed 10 years ago

#14111 closed bug (notabug)

.data(obj) sets data in weird way that ignores next .data(key,value) call

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


Steps to reproduce: Go to http://jsbin.com/uzecoq/5 Click on the title three times slowly. The first click should do nothing.

Expected result: The alerted message should appear on the second click.

Observed result: The alerted message does not appear until the third click!

The following is the incorrect code: obj.data({'foo':false}); obj.data('foo',true); obj.data('foo'); returns false instead of true!!

However if I do either of these two it works great: obj.data({'foo':false}); obj.data('foo',true); obj.data('foo',true); Have to assign twice for it to stick.. obj.data('foo'); returns true --OR-- obj.data('foo',false); Don't use .data(obj) and there is no issue! obj.data('foo',true); obj.data('foo'); returns true

Change History (3)

comment:1 Changed 10 years ago by dmethvin

The examples using "foo" should work with no problem. The issue is that you've used a hyphenated form of a name when setting the data object in the code sample. You always need to use the W3C camelCased form when setting data directly.


It's mentioned here:

Rule: When accessing data taken from data-* attributes, and especially when accessing the data object directly, use the W3C camelCasing conventions. -- http://www.learningjquery.com/2011/09/using-jquerys-data-apis

Since your call to .data() *explicitly* requests the dashed form, that value is returned to you even though the normalized camelCased value has been changed by a subsequent call.

comment:2 Changed 10 years ago by anonymous

My bad.

Thanks for getting back to me so fast :).

I can't close the ticket, but I feel satisfied that this resolved the issue.

comment:3 Changed 10 years ago by timmywil

Resolution: notabug
Status: newclosed
Note: See TracTickets for help on using tickets.