Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  117] [ 1]  / answers: 1 / hits: 60728  / 13 Years ago, wed, february 8, 2012, 12:00:00

According to the dataset specification, 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 becomes this in Chrome and Firefox:


<p id="example" 
data-a="null"
data-b="undefined"
data-c="false"
data-d="3"
data-e="1,2,3"
data.f="[object Object]"
data.g="{"prop":"value"}"
></p>

The Chrome/Firefox implementations mimic setAttribute. It basically applies .toString() first. This makes sense to me except for the treatment of null because I would expect that null would remove the attribute. Otherwise how does the dataset API do the equivalent of:


elem.removeAttribute('data-a');

And what about boolean attributes:


<p data-something> is equivalent to <p data-something=""> Hmm.


More From » dom

 Answers
2

Wouldn't 'delete' remove dataset element? E.g.:



<div id=a1 data-foo=bar>test</div>

<script>
var v = document.getElementById('a1');
alert(v.dataset.foo);
delete v.dataset.foo;
alert(v.dataset.foo);
</script>

[#87585] Tuesday, February 7, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brittanye

Total Points: 263
Total Questions: 94
Total Answers: 115

Location: Burkina Faso
Member since Thu, Dec 23, 2021
3 Years ago
brittanye questions
Mon, Aug 10, 20, 00:00, 4 Years ago
Tue, Jun 16, 20, 00:00, 4 Years ago
Wed, Apr 22, 20, 00:00, 4 Years ago
Mon, Apr 13, 20, 00:00, 4 Years ago
;