Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-12-2012, 05:49 PM   PM User | #1
johnsmith153
New Coder

 
Join Date: Mar 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
johnsmith153 is infamous around these parts
Remove ALL attributes

I can't seem to find a simple way to do this.

$('.section').removeAllAttr() would be nice.

$('.section').removeAttr() doesn't work where I thought it might.

So,
Code:
<div class="section hello" data-test="123" id="an-id" data-something="abc">
content
</div>
...results in...
Code:
<div>
content
</div>
It needs to work for any tag as well.

Surely this should be easy but I just can't do it.

If it's easier (or quicker for jQuery) then removing all the data- attributes is fine, as I could then do this:
Code:
$('.section').removeAllDataAttributes().removeClass();
...which would be fine for what I need.
johnsmith153 is offline   Reply With Quote
Old 12-12-2012, 05:58 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
This little plug-in should be able to help you
Code:
jQuery.fn.removeAttributes = function() {
  return this.each(function() {
    var attributes = $.map(this.attributes, function(item) {
      return item.name;
    });
    var elem = $(this);
    $.each(attributes, function(i, item) {
      elem.removeAttr(item);
    });
  });
}
Then you can just call
Code:
$('.section').removeAttributes();
devnull69 is offline   Reply With Quote
Old 12-12-2012, 06:12 PM   PM User | #3
johnsmith153
New Coder

 
Join Date: Mar 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
johnsmith153 is infamous around these parts
Great thanks.

I actually saw that on Google somewhere but it looks too long winded. As long as it's as quick as a built-in jQuery function then I'm fine. Is it as quick? I have no idea.

Is there a quicker way at all?

I don't want to implement something for this that will slow the initial load down.
johnsmith153 is offline   Reply With Quote
Old 12-12-2012, 06:18 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
A lot of the jQuery calls would run a lot more JavaScript in the browser than that - jQuery runs lots of JavaScript in order to save you from having to write it yourself.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:45 PM.


Advertisement
Log in to turn off these ads.