3

I've been looking at some apis and plugins and I've been seeing plugins that look like this

<div class="fb-like" data-href="http://developers.facebook.com/docs/reference/plugins/like" data-send="true" data-width="450" data-show-faces="true"></div>

or

<a class="bistri-button" data-key="871b7d8f839e9cdbfc6dcc87b6932889feae334d8fbc2862"></a>

What does dat-key or data-send represent ? How do you use it? Is it a way to send data to the javascript file handling the request? Then how can you handle it in the javascript file?

Sven
  • 69,403
  • 10
  • 107
  • 109
Michael Nana
  • 1,969
  • 4
  • 24
  • 36
  • 2
    unless you access it or use it, it does nothing. It's just a way of storing data in html in a way that doesn't invalidate your html while possibly being descriptive/expressive. – Kevin B Jul 09 '13 at 20:42

6 Answers6

3

They are HTML5 custom data attributes.

http://www.w3.org/TR/2011/WD-html5-20110525/elements.html#custom-data-attribute

Petah
  • 45,477
  • 28
  • 157
  • 213
2

data-* attributes are HTML-5 custom attributes

Names that are not default attribute names can be used by prepending the data- to make the HTML valid.

They can be retrieved by either using

$('.fb-like').attr('data-key')

or

$('.fb-like').data('key') // This is more appropriate
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
2

It's just an attribute. Instead of setting arbitrary attributes on an element, they added data-*. They can contain whatever you want and have no inherit special meaning.

In jQuery, they can be accessed using .data():

$('.fb-like').data('href')
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Data is the good way to add custom attributes to your HTML tags.

You can also acces them with the jquery function .data()

$(element).data('key');//Will get the value of data-key
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

data-* attributes is a way to attach some information to HTML element.

It doesn't do anything out of the box or affect behavior anyhow. However, it provides standards compliant way to assign some data to element.

Alexander Puchkov
  • 5,913
  • 4
  • 34
  • 48
0

You can easily get or set the values of data- attributes with jQuery. Examples are shown in the link below.

http://api.jquery.com/jQuery.data/