htmljquerycustom-data-attributejquery-data

Are HTML data attributes and jQuery's .data() same thing?


In HTML it's possible to set custom 'data-' attributes to elements like

<div data-blah="blah">

The jQuery framework has methods to do similar things (.data() etc). My question is do the jQuery methods just manipulate and read these html5 data attributes, or they are referring to different jQuery-type data-attributes?


Solution

  • No, totally different things. jQuery's .data() existed even before W3C added these to HTML version 5.

    See https://api.jquery.com/data/ for details.

    To get an HTML data- attribute value from an HTML tag, try:

    jQuery('selector here').attr('data-blah');