Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing data attributes from HTML using jQuery

Can't seem to get this one to work...

I have a page that hides certain links. When the DOM is loaded, I'm using jQuery to toggle some of those elements. This is driven by using a data attribute like so:

<div class="d_btn" data-usr='48'>
  <div class="hidden_button">

Then, I have the code:

  $.each($(".d_btn"), function() {
     var btn = $(this).data('usr');
   if ( btn == '48' ){      
     $(this).children('.hidden_button').toggle();
   }

The above all works as planned. The problem is that I am trying to remove the data-usr from the class .d_btn once the if statement is evaluated. I've tried the following and nothing works (i.e., after the page is loaded, the source still shows the data-usr attribute:

$(this).removeAttr("data-usr");

$(this).removeData("usr");

I've been working on this for a couple of hours now and...nothing! Help is greatly appreciated!

UPDATE

I've tried the great suggestions of setting the data attribute to an empty string but I'm still not getting the desired result.

To explain a little further, The reason I'm trying to remove the attribute is so when an ajax response adds another item to the page, the previously added items would already have the button either shown or hidden. Upon AJAX response, I'm calling the same function once the DOM is loaded.

Currently, when something is added via AJAX, it toggles all the buttons (showing the ones that were hidden and vice versa.) Ugh...

I'm also fully willing to try alternatives to my approach. Thanks!

UPDATE

Well, the light bulb just flashed and I am able to do what I want to do by just using .show() instead of .toggle()

Anyway, I'd still like to find an answer to this question because the page will be potentially checking hundreds of items whenever something is added - this seems horribly inefficient (even for a computer, hahaha.)

like image 627
Angelo Chrysoulakis Avatar asked Sep 20 '12 23:09

Angelo Chrysoulakis


People also ask

How remove data attribute in jQuery?

The removeAttr() method is an inbuilt method in jQuery which is used to remove one or more attributes from the selected elements.

How do you add or remove HTML attribute in jQuery?

To add and remove HTML attributes with jQuery, use the addClass() and removeClass() method.

How remove selected attribute from option in jQuery?

$("#mySelect option:selected"). each(function () { $(this). removeAttr('selected'); });

How do I delete data attributes?

Make use of the removeAttribute() method to delete the given data attribute: el. removeAttribute('data-foo'); Apart from setting, getting, and erasing data values, all three methods are also used for manipulating other element attributes.


1 Answers

Why don't you set the value to a random value or empty variable instead if removeAttr does not work..

$(this).attr("data-usr" , '');

$(this).prop("data-usr" , '');
like image 154
Sushanth -- Avatar answered Sep 22 '22 17:09

Sushanth --