javascript - How to remove data- elements from html - Stack Overflow

I have an href with data-toggle="dropdown", which I'm trying to remove.This snippet doe

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 the dataset 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
Add a ment  | 

3 Answers 3

Reset to default 4

If 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");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742377413a4432465.html

相关推荐

  • javascript - How to remove data- elements from html - Stack Overflow

    I have an href with data-toggle="dropdown", which I'm trying to remove.This snippet doe

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信