Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving HTML data-attributes using jQuery

Tags:

html

jquery

How can I get the values stored in the data attributes using jQuery ?

<div class="sm-tot" data-ts-speed="500" data-ts-interval="4000" data-ts-newVal="5000" > 
like image 482
Morais Avatar asked Mar 15 '12 13:03

Morais


People also ask

How use HTML data attribute in jQuery?

Alternatively, you can also use the jQuery data() method (jQuery version >= 1.4. 3), to get the data-attribute of an element using the syntax like $(element). data(key) . That means in the above example to get the data-id using data() method you can use the statement like $(this).

How get data attribute value in jQuery?

You can use this jquery attr() syntax for get data-id attribute value. $("selector"). data("data-textval"); You can use this jquery attr() syntax for get data-textval attribute value.

How do I find data attributes?

To get a data attribute through the dataset object, get the property by the part of the attribute name after data- (note that dashes are converted to camelCase). Each property is a string and can be read and written.

How do you check if data attribute exists in jQuery?

The jQuery. hasData() method provides a way to determine if an element currently has any values that were set using jQuery. data() . If there is no data object associated with an element, the method returns false ; otherwise it returns true .


1 Answers

Use the jQuery .data() function:

var speed = $("yourdiv").data("ts-speed"); 
like image 84
Alytrem Avatar answered Sep 28 '22 19:09

Alytrem