Best practices for data association with HTMLElement objects?
Asked Answered
P

6

7

I came across 3 ways to store any data with HTMLElement object.

Can someone please suggest the best practice to associate any data with element object?

I prefer number 3 because it doesn't set any HTML attribute as in the case of 1 and 2. It's just like setting and getting any property on the object.

  1. Use setAttribute('nonStandardDataProperty')
  2. Use dataset property of HTMLElement object for example dataset.x for data-xattribute
  3. HTMLElement is object, so define any property and it will serve as data storage for that element
Piecemeal answered 2/4, 2013 at 13:47 Comment(6)
Isn't 1 and 3 the same?Birdiebirdlike
Option 3 is object only after you grab a reference on it, this eliminates getting associated data from the server.Fadeout
@NilsH: No, only in extinct IEs. Read prop vs attrSimms
@Birdiebirdlike I think option 1 creates HTML element attribute that can be further accessed by getAttribute method while 3rd has nothing to do with attributePiecemeal
Are we talking about associating data with DOM elements or with HTML "elements"? Or potentially both?Shaeshaef
@Bergi: I mean associate any data with element object(HTMLElement object), obviously not all node objects which include text and commentsPiecemeal
R
2

Option #2 seems to me to be the most "standards-compliant", if that's what you're looking for; plus, it allows you to set those attributes from within the HTML while still maintaining valid markup. It's generally my preference, but it's really whatever works best for you in your situation: if it works, go with it.

Radiolucent answered 2/4, 2013 at 14:14 Comment(0)
C
1

I would use option #1 because it's the most portable.

However I would use HTML5's data- prefix for those custom attributes for compatibility with jQuery's .data() method.

Celom answered 2/4, 2013 at 14:12 Comment(3)
@FelixKling not in my experience if you use .data to write the data in the first place. That stores the data as a property rather than as an attribute.Celom
@Alnitak: jQuery's .date() method does not use HTML dataset, where you can only store strings (just as in the data attributes).Simms
I was actually referring to the first option, i.e. using getAttribute. Sorry for not having been specific enough.Shaeshaef
S
0

The third option is storing data in the DOM which might not be a bad idea if the data is not huge.If it is, then storing and retrieval of data might affect performance of the app.

Stoplight answered 2/4, 2013 at 13:59 Comment(1)
@FelixKling Nice article with good reasons but it's sad that we are not allowed to use prototypal inheritance which is the real power of JavaScript.Piecemeal
G
0

I guess the best way is using HTML5 data-* Attributes and jQuery's .data() function. It will probably have the best way to store data in HTML elements built-in and update to latest technologies in the future, so you can lean back and just be productive

<div id="myDiv" data-my-var="my-var-value"></div>

can be used in JavaScript like this: (jQuery required)

console.log( $( '#myDiv' ).data( 'my-var' ) )

Edit: and set like this

$( '#myDiv' ).data( 'my-var', 'my-new-var-value' );

which will also update the data-* attribute in this case

Graber answered 2/4, 2013 at 14:16 Comment(0)
K
0

Option 4: store an identifier on the DOMNode which you can use to look things up in a detached map (yes, this is how jQuery does it - on top of importing all data-* attributes during init, of course).

Going with #3 is fine if you mind your property names. #2 should only allow Integer and String values, #1 might have the same issue.

I'd go with #4, simply because I don't like the odds of a new spec popping up and claiming a property name I used for my own data…

Kelleher answered 2/4, 2013 at 14:18 Comment(2)
thanks. Can you please elaborate "look things up in a detached map" ?Piecemeal
Notice that this might have garbage collection problems.Simms
B
-1

Twitter Bootstrap uses option one.

Take a look at Twitter Bootstrap document, you will see plenty uses of storing data in the none-standard property html elements.

example

<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="dLabel">
  ...
</ul>


<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
Bistoury answered 2/4, 2013 at 14:7 Comment(3)
I don't see any non-standard attribute name in your html snippet.Simms
is aria-labelledby standard?Bistoury
ok i got it. I were wrong. Now i'm confused between option 1 and 2. What is this approach called?Bistoury

© 2022 - 2024 — McMap. All rights reserved.