Created
November 2, 2015 00:16
-
-
Save AurelioDeRosa/a05c84b7e7fd9fa3630f to your computer and use it in GitHub Desktop.
Simple shim for the Dataset API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shim for the Dataset API to set and get data | |
var data = (function() { | |
var supported = 'dataset' in document.createElement('div'); | |
return function(element, property, value) { | |
if (arguments.length === 2) { | |
return supported ? element.dataset[property] : element.getAttribute('data-' + property); | |
} else if (arguments.length === 3) { | |
return supported ? element.dataset[property] = value : element.setAttribute('data-' + property, value); | |
} | |
}; | |
})(); | |
// Shim for the Dataset API to remove data | |
var removeData = (function() { | |
var supported = 'dataset' in document.createElement('div'); | |
return function(element, property) { | |
return supported ? delete element.dataset[property] : element.removeAttribute('data-' + property); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment