views:

491

answers:

4

Is it possible to remove all attributes at once using jQuery?

<img src="example.jpg" width="100" height="100">

to

<img>

I tried $('img').removeAttr('*'); with no luck. Anyone?

+6  A: 

Update: the previous method works in IE8 but not in IE8 compatibility mode and previous versions of IE. So here is a version that does and uses jQuery to remove the attributes as it does a better job of it:

$("img").each(function() {
  // first copy the attributes to remove
  // if we don't do this it causes problems
  // iterating over the array we're removing
  // elements from
  var attributes = $.map(this.attributes, function(item) {
    return item.name;
  });

  // now use jQuery to remove the attributes
  var img = $(this);
  $.each(attributes, function(i, item) {
    img.removeAttr(item);
  });
});

Of course you could make a plug-in out of it:

jQuery.fn.removeAttributes = function() {
  return this.each(function() {
    var attributes = $.map(this.attributes, function(item) {
      return item.name;
    });
    var img = $(this);
    $.each(attributes, function(i, item) {
    img.removeAttr(item);
    });
  });
}

and then do:

$("img").removeAttributes();
cletus
No need for a plugin, it was the while loop I was after. Thanks!
David
Err, no. This doesn't come close to a working solution for IE. `removeAttribute` does not work at all like you expect here (hint: that `while` loop is gonna lock up the poor browser forever).
Crescent Fresh
+1  A: 

Why would you need to do this at all? What is the purpose of having an empty img? Maybe what you really need is to remove it or if you're searching to reset width and height, it's better to set this things with CSS and just switch classes.

Pavel Dubinin
:-) I just commented the same thing.
sjobe
i need it, because I want to load a new image source in the same tag without loosing events.
David
Ok, but why do you remove attributes? Just set another src value:$('img').attr('src','your new src here');
Pavel Dubinin
I need to remove width,height and style as well. I could do it manually, I was just interested if there was a quicker way.
David
As I already mentioned, it's easier to do this with switching classes. Let the class have all the widht, height and whatever else and then just:$('img').attr('src','your new src here'); $('img').addClass('someclass');
Pavel Dubinin
A: 

I don't know exactly what you're using it for, but have you considered using css classes instead and toggling those ? It'll be less coding on your side and less work for the browser to do. This will probably not work [easily] if you're generating some of the attributes on the fly like with and height.

sjobe
A: 

Bad idea, you should only remove what attributes you know about, a DOM element has a lot more attributes than what you specify in the tag each with their own default or calculated value.

row1
row1 has a point. You may one day in the future want to add a CSS class to the image only to find that your old code now needs to be rewritten...
Funka
I have full control of the element. I need to remove several or all attributes and I was interested in finding out if jQuery had a method for it.
David