Removing All HTML Element Attributes at Once

Sometimes you are required to remove all attributes from HTML element. There is no native JavaScript method to achieve this. However, we can definitely do that with a single loop, iterating over HTML element attributes removing each on the way.

It is important to point out that we have to loop the array in a reverse direction (starting with the last item) to avoid getting an error due to the changing length of the array while removing items. When starting removing from the last item, updated array length doesn't matter, because the next item in the loop is a previous one.

Remove All Attributes at Once Using jQuery

For jQuery users, probably the best implementation would be to extend its existing .removeAttr() function making it accept zero parameters to remove all attributes from each element in a set.

To sum up the logic, you can safely remove items from any array while iterating it in a reverse direction. Reverse looping will assure that array length decreased on every removal is not going to cause you errors.