Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeData() jquery method not working

I think I'm using removeData correctly but it doesn't seem to be working, here's what I'm seeing in the dev console, could anyone explain what I'm doing wrong?

I'm outputting the current data attribute value, calling removeData, then outputting the value again and its still there.

$('.questionList > li').eq(1).data('fieldlength')
3
$('.questionList > li').eq(1).removeData('fieldlength');
[
<li class=​"questionBox" data-createproblem=​"false" data-fieldlength=​"3" data-picklistvalues data-required=​"true" data-sfid=​"a04d000000ZBaM3AAL" data-type=​"Text">​
<div class=​"questionLabel">​Birthdate​</div>​
</li>​
]
$('.questionList > li').eq(1).data('fieldlength')
3
like image 316
turbo2oh Avatar asked May 17 '13 18:05

turbo2oh


People also ask

How remove data attribute value in jQuery?

jQuery removeAttr() Method The removeAttr() method removes one or more attributes from the selected elements.

How add data attribute in jQuery?

$('div'). attr('data-info', 1); $('div')[0]. setAttribute('data-info',1); In order to access an element with the attribute set, you can simply select based on that attribute as you note in your post ( $('div[data-info="1"]') ), but when you use .


3 Answers

There is bit of gotcha I wanted to clarify in case anyone else stumbles upon it...

If you have HTML5 data-* attributes on an element you need to use jQuery's removeAttr() instead of removeData() if you want to remove them from the element in the DOM.

For example, to actually remove a data attribute from an element you need to use:

$({selector}).removeAttr('data-fieldlength');

You can read values like this with $({selector}).data('fieldlength') but removeData() doesn't actually remove them if they are HTML attributes on an element present in the source of the page (it just deletes it in memory, so that if you query it again with jQuery it appears to have been removed).

Personally I think this behaviour is broken and I'm sure catches a lot of people out.

like image 65
Iain Collins Avatar answered Oct 19 '22 03:10

Iain Collins


It's because your data originates in the HTML data-fieldlength attribute. According to the docs:

When using .removeData("name"), jQuery will attempt to locate a data- attribute on the element if no property by that name is in the internal data cache. To avoid a re-query of the data- attribute, set the name to a value of either null or undefined (e.g. .data("name", undefined)) rather than using .removeData().

So instead of

$('.questionList > li').eq(1).removeData('fieldlength');

you should do

$('.questionList > li').eq(1).data('fieldlength',null);
like image 38
Blazemonger Avatar answered Oct 19 '22 05:10

Blazemonger


.removeData() will only remove data from jQuery's internal .data() cache so any corresponding data- attributes on the element will not be removed. A later call to data() will therefore re-retrieve the value from the element's data- attribute. To prevent this, use .removeAttr() alongside .removeData() to remove the data- attribute as well.

Example:

$('div').removeData('info');
$('div').removeAttr('data-info');

Then set either:

$('div').data('info', 222);
$('div').attr('data-info', 222);
like image 4
ztrat4dkyle Avatar answered Oct 19 '22 05:10

ztrat4dkyle