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,
<div class="section hello" data-test="123" id="an-id" data-something="abc">
content
</div>
…results in…
<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:
$('.section').removeAllDataAttributes().removeClass();
…which would be fine for what I need.
Hi,
Unfortunately, there’s nothing like $('.section').removeAllAttr()
But, you can do it like this:
$(document).ready(function() {
$('div').each(function() {
var attributes = this.attributes;
var i = attributes.length;
while( i-- ){
this.removeAttributeNode(attributes[i]);
}
})
});
The following is a much simpler way of achieving the same result.
$('.section').replaceWith(function () {
return $('<div>').append($(this).contents());
});
Or if you want to make it fully generic, so that it uses the same element, you can use this.nodeName instead:
$('.section').replaceWith(function () {
return $('<' + this.nodeName + '>').append($(this).contents());
});