Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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:
Blocked by: Blocking:

Description

simple example:

html:

<div id="d" data-k1="v1">&nbsp;</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 (5)

comment:1 Changed 7 years ago by Rick Waldron

Resolution: wontfix
Status: newclosed

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).

comment:2 in reply to:  1 Changed 7 years 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 7 years ago by scottgonzalez

Resolution: wontfix
Status: closedreopened

comment:4 Changed 7 years ago by scottgonzalez

Component: unfiledattributes
Resolution: worksforme
Status: reopenedclosed

Works for me: http://jsfiddle.net/LxmLh/

Tested in Chrome and Firefox.

comment:5 Changed 7 years ago by Rick Waldron

That's what I get for closing tickets at the tail end of working 12 hours straight... Sorry!

Note: See TracTickets for help on using tickets.