I have an href with data-toggle="dropdown", which I'm trying to remove. This snippet doesn't seem to work:
$(document).on('click','a',function() {
$(this).data('toggle','Phillip Senn');
});
Instead, Firebug is showing that data-toggle still equals "dropdown" and there is a new "toggle" attribut equal to "Phillip Senn".
What I'd like to do is remove it altogether.
I have an href with data-toggle="dropdown", which I'm trying to remove. This snippet doesn't seem to work:
$(document).on('click','a',function() {
$(this).data('toggle','Phillip Senn');
});
Instead, Firebug is showing that data-toggle still equals "dropdown" and there is a new "toggle" attribut equal to "Phillip Senn".
What I'd like to do is remove it altogether.
Share Improve this question edited Apr 6, 2012 at 0:54 Phillip Senn asked Apr 6, 2012 at 0:37 Phillip SennPhillip Senn 47.7k91 gold badges261 silver badges378 bronze badges 4- If you showed us your HTML, we might know that you had a data-toggle attribute on the HTML element that also has to be addressed. Come on people, if you don't show us your HTML, you WONT necessarily get a plete or best answer - always. – jfriend00 Commented Apr 6, 2012 at 0:50
-
What do you mean by remove it from HTML? Are you actually trying to remove the attribute from the
a
element? Or the property from thedataset
of the element? Or do you want to clear it from jQuery's.data()
store? These are all different. – user1106925 Commented Apr 6, 2012 at 0:51 - Ah. I found a solution. I can use the removeAttr('data-toggle') to remove it. – Phillip Senn Commented Apr 6, 2012 at 0:55
- Yeah, I thought they were the same. – Phillip Senn Commented Apr 6, 2012 at 0:55
3 Answers
Reset to default 4If you've got an attribute on the HTML and you want to remove both it and the jQuery data, then you would use this:
this.removeAttribute("data-toggle");
$(this).removeData('toggle');
Only, the jQuery data is updated with subsequent writes to .data()
, but if you want to remove the attribute that was originally on the HTML object, then you have to call removeAttribute()
too.
removeData
I do not understand how people can spend time posting a question when googling jquery remove data and clicking the first result would give them the answer in only a fraction of the time...
I think it goes something like this...
$(this).removeData('toggle');
To remove an attribute:
$(this).removeAttr("the-name-of-the-attribute");