Last active
August 29, 2015 14:11
-
-
Save uadev/b801cd123cc81ae2d8ce to your computer and use it in GitHub Desktop.
Quick dataset function
This file contains 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
var dataset = function initDataSet() { | |
if (document.documentElement.dataset) { | |
return function native(el, prop, value) { | |
if (typeof value === 'undefined') { | |
return el.dataset[prop] = value; | |
} | |
else { | |
return el.dataset[prop]; | |
} | |
} | |
} | |
else { | |
return function poly(el, prop, value) { | |
if (typeof value === 'undefined') { | |
return el.setAttribute('data-' + prop, value); | |
} | |
else { | |
return el.getAttribute('data-' + prop); | |
} | |
} | |
} | |
}(); |
This file contains 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
var dataget = function initDataGet() { | |
if (document.documentElement.dataset) { | |
return function native(el, prop) { | |
return el.dataset[prop] = value; | |
} | |
} | |
else { | |
return function poly(el, prop, value) { | |
return el.setAttribute('data-' + prop); | |
} | |
} | |
} |
This file contains 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
var dataset = function initDataSet() { | |
if (document.documentElement.dataset) { | |
return function native(el, prop, value) { | |
return el.dataset[prop] = value; | |
} | |
} | |
else { | |
return function poly(el, prop, value) { | |
return el.setAttribute('data-' + prop, value); | |
} | |
} | |
}(); |
Note: For older browser usage is limited by Element.getAttribute
method
https://developer.mozilla.org/en-US/docs/Web/API/Element.getAttribute#Notes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: