Extends the [Element][] native object with methods useful for working with HTML5 data properties.
Sets a value for a given data property.
myDiv.setData(name, value)
myDiv.setData('foo-bar', 'baz');
//result: <div data-foo-bar="baz"></div>
- (element) the element
Gets a value for a given data property.
myDiv.getData(name, defaultValue)
- name - (string) the data property to get; this is prepended with "data-".
- defaultValue - (string, number) the value to assign if none is set.
myDiv.getData('foo-bar');
//returns "baz" from: <div data-foo-bar="baz"></div>
- (string) the value if found, otherwise null.
Sets a value for a given data property, encoding it into JSON.
myDiv.setJSONData(name, object)
myDiv.setJSONData('foo-bar', [1, 2, 'foo','bar']);
//result: <div data-foo-bar='"[1, 2, \'foo\', \'bar\']"'></div>
- (element) the element
Gets a value for a given data property, parsing it from JSON.
myDiv.getJSONData(name, strict, defaultValue)
- name - (string) the data property to get; this is prepended with "data-".
- strict - (boolean) if true, will set the JSON.decode's secure flag to true; otherwise the value is still tested but allows single quoted attributes.
- defaultValue - (string, number) the value to assign if none is set.
myDiv.getData('foo-bar');
//returns [1, 2, 'foo','bar'] from: <div data-foo-bar='"[1, 2, \'foo\', \'bar\']"'></div>
- (object) the value if found, otherwise null.