Tag: html5 data

using $.data(“value”) versus $.attr(“data-value”)

When I use jQuery.data to change the value the attribute in the DOM doesn’t change. But I like when the attribute in the DOM changes, its much easier to debug stuff. So is it ok to use $().attr(“data-value”) to change data-attributes? Just a little fiddle to show that mixing $().attr and $().data won’t work well […]

Why does jquery remove brackets from data html5 attribute?

I am trying to get the content of a data attribute with jquery but returned data is not what I had set. With this simple example: <div id=”test” data-test=”[1]”></div> But $(‘#test’).data(‘test’) returns 1 instead of [1] No problem using pure javascript. View it online: https://jsfiddle.net/jojhm2nd/

Using jQuery to get data attribute values with .each()

I have the following HTML with data attributes – I want to write some jQuery that will loop through the HTML and collect the data attributes and put them into an array – could anyone assist as I’m getting an error. ERROR in console log : item.data is not a function I am trying to […]

How to remove data-* attributes using HTML5 dataset

According to the dataset spec, how is element.dataset meant to delete data attributes? Consider: <p id=”example” data-a=”string a” data-b=”string b”></p> If you do this: var elem = document.querySelector(‘#example’); elem.dataset.a = null; elem.dataset.b = undefined; elem.dataset.c = false; elem.dataset.d = 3; elem.dataset.e = [1, 2, 3]; elem.dataset.f = {prop: ‘value’}; elem.dataset.g = JSON.stringify({prop: ‘value’}); the DOM […]

store and retrieve javascript arrays into and from HTML5 data attributes

How can a javascript Array be stored in an HTML5 data attribute? I’ve tried every variation of JSON.stringifycation and escaping characters. What is the precise method to store the array and retrieve it again? note I build the array with [ $(“#firstSelectedElement”).val(), $(“#secondSelectedElement”).val() ]. I retrieve id=”storageElement” data-storeIt=”stuff” with $(“#storageElement”).data(‘storeit’). I can never seem to […]