Is there some way that I can run the following:
var data = $("#dataTable").data('timer'); var diffs = []; for(var i = 0; i + 1 < data.length; i++) { diffs[i] = data[i + 1] - data[i]; } alert(diffs.join(', '));
Only if there is an attribute called data-timer on the element with an id of #dataTable?
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 .
Using jQuery The idea is to use the . attr() method, which returns the attribute's value for an element if it is present and returns undefined if the attribute doesn't exist.
if ($("#dataTable").data('timer')) { ... }
NOTE this only returns true
if the data attribute is not empty string or a "falsey" value e.g. 0
or false
.
If you want to check for the existence of the data attribute, even if empty, do this:
if (typeof $("#dataTable").data('timer') !== 'undefined') { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With