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
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
P K
  • 9,972
  • 12
  • 53
  • 99
  • Isn't 1 and 3 the same? – NilsH Apr 02 '13 at 13:48
  • Option 3 is object only after you grab a reference on it, this eliminates getting associated data from the server. – Valentin V Apr 02 '13 at 13:50
  • @NilsH: No, only in extinct IEs. Read [prop vs attr](http://stackoverflow.com/q/5874652/1048572) – Bergi Apr 02 '13 at 13:52
  • @NilsH I think option 1 creates HTML element attribute that can be further accessed by getAttribute method while 3rd has nothing to do with attribute – P K Apr 02 '13 at 13:53
  • Are we talking about associating data with DOM elements or with HTML "elements"? Or potentially both? – Felix Kling Apr 02 '13 at 13:56
  • @Bergi: I mean associate any data with element object(HTMLElement object), obviously not all node objects which include text and comments – P K Apr 02 '13 at 13:58

6 Answers6

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.

Adrian
  • 42,911
  • 6
  • 107
  • 99
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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • @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. – Alnitak Apr 02 '13 at 14:26
  • @Alnitak: jQuery's `.date()` method does not use HTML [dataset](https://developer.mozilla.org/en-US/docs/DOM/element.dataset), where you can only store strings (just as in the data attributes). – Bergi Apr 02 '13 at 15:53
  • I was actually referring to the first option, i.e. using `getAttribute`. Sorry for not having been specific enough. – Felix Kling Apr 02 '13 at 16:32
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.

  • @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. – P K Apr 02 '13 at 14:31
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

Torben
  • 193
  • 11
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…

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
-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>
lngs
  • 690
  • 1
  • 8
  • 17