Skip to main content

Bug Tracker

Side navigation

#12323 closed bug (worksforme)

Opened August 16, 2012 11:31PM UTC

Closed August 17, 2012 12:27AM UTC

Last modified August 17, 2012 01:43AM UTC

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"> </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)

Attachments (0)
Change History (5)

Changed August 17, 2012 12:05AM UTC by rwaldron comment:1

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

Changed August 17, 2012 12:20AM UTC by anonymous comment:2

Replying to [comment:1 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.

Changed August 17, 2012 12:26AM UTC by scottgonzalez comment:3

resolution: wontfix
status: closedreopened

Changed August 17, 2012 12:27AM UTC by scottgonzalez comment:4

component: unfiledattributes
resolution: → worksforme
status: reopenedclosed

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

Tested in Chrome and Firefox.

Changed August 17, 2012 01:43AM UTC by rwaldron comment:5

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