Ticket #12323 (closed bug: worksforme)
elment.attr('data-*, value) does not update element.dataset if data-* key does not exist
| Reported by: | anonymous | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | None |
| Component: | attributes | Version: | 1.8.0 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
simple example:
html:
<div id="d" data-k1="v1"> </div>
javascript:
/* correctly sets $('#d').context.dataset.k1 to 'some value' */
$('#d').attr('data-k1', 'some value');
/* $('#d').context.dataset.k2 is undefined */
$('#d').attr('data-k2', 'some other value');
/* $('#d').context.dataset.k2 is set to 'some other value' */
$('#id').context.setAttribute('data-k2', 'some other value');
I was able to work around the issue by using the native setAttribute method.
I was also able to work around this by setting the dataset property manually, but according to the w3c spec, it's supposed to be a read only property. None of the browsers tested enforce this however.
Browsers tested on: Firefox, Chrome and Opera (ie doesn't support the dataset property)
Change History
comment:1 follow-up: ↓ 2 Changed 9 months ago by rwaldron
- Status changed from new to closed
- Resolution set to wontfix
comment:2 in reply to: ↑ 1 Changed 9 months ago by anonymous
Replying to rwaldron:
jQuery's data() functionality existed long before dataset and has it's own semantic behaviour to adhere to, and it's expected to be fast. A DOM operation will slow it down tremendously (as well as having no effect in browsers that don't support it).
I understand what you are saying about the data() functionality, and I use the data() functionality as well, and it is a nice. I like it, but it is not the optimal solution for what I need to do, nor does it really have anything to do with this topic.
I'm talking about the attr() function, which does work as expected if an attribute is already set for the DOM node, as noted above. Which would imply that attr() is already modifying the DOM.
I was reporting it to be a good citizen, but it is good to know you have no intentions of fixing it, so I'll just make it policy to not use the attr method and instead use the native setAttribute method on the dom node, no big deal to me, I didn't bang my head on the issue that long.
comment:3 Changed 9 months ago by scott.gonzalez
- Status changed from closed to reopened
- Resolution wontfix deleted
comment:4 Changed 9 months ago by scott.gonzalez
- Status changed from reopened to closed
- Resolution set to worksforme
- Component changed from unfiled to attributes
Works for me: http://jsfiddle.net/LxmLh/
Tested in Chrome and Firefox.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

jQuery's data() functionality existed long before dataset and has it's own semantic behaviour to adhere to, and it's expected to be fast. A DOM operation will slow it down tremendously (as well as having no effect in browsers that don't support it).