Method _.dataGet
Recovers the value of a data- attribute. Can optionally split the value from a comma delimited list to an array.
This makes it easier in legacy browsers to access data- attributes since the dataList
array doesn't exist. I also find it handy to pass lists of instructions comma delimited in my data attributes.
- Calling Convention:
-
_.dataGet(Element, dataName[, split])
- Parameters:
-
- Element
-
The Element to pull the
data-dataName
from. - dataName
- The suffix/data index
- split optional
- If set to a loose true, the value recovered will be split into an array by comma. Otherwise the result will be returned as is.
- Returns:
-
The contents of the corresponding
data-dataName
attribute if present on Element. If no value is present a boolean false is returned.
Example
HTML
<div id="test" data-sample="hello"></div>
JavaScript
Console.log(_.dataGet(_d.getElementById('test'), 'sample'));
Output
hello