Many a time you might have come across a scenario where you would want to remove multiple attributes from an element. For example, consider the following DIV, <div id="#rightSection" data-id="xyz" data-module="abc"> ... </div>. Now using jQuery’s removeAttr() method you want to remove both the data-id and data-module attributes. Many of us try to use multiple calls to the removeAttr() method by using jQuery’s chaining feature. Something like $(‘#rightSection’).removeAttr(‘data-id’).removeAttr(‘data-module’). However, while this may work, this is not a very optimal way to write the code.

So, How do I remove multiple attributes or How do I Combine multiple .removeAttr() statements?

The answer to that is very easy one. You can remove multiple attributes in jQuery by separating them using spaces. This means that you would have to pass all the attributes you want to remove, separated by space, to the removeAttr() method. So now your code should look similar to the following :

This way, you don’t have to repeat the .removeAttr() method to remove each attribute individually. And now you can actually Write Less and Do More :) .

Do let us know if you liked the solution. Please like and share and help your friends to learn about this !

Related Posts

  • What is the difference between .prev() , .prevAll() and .prevUntil() jQuery APIs ? jQuery has made a drastic change in the way JavaScript are written. It has given more scripting power to the developers and the ability to “write less and do more” . […]
  • How to check jQuery version? Many of us might have come across scenarios wherein we wanted to do something based on the version of the jQuery loaded. To be specific I'm talking about scenarios […]
  • How to make placeholder work in IE ? One of the interesting features of HTML5 is the "placeholder" attribute. This new attribute has helped web developers save a lot of time as without it we would have had […]
  • How to center align a Div ? One of the most common queries all the new front End developers have is " How do we center align a DIV? ". Well, let me tell you that it is one of the easiest task. […]
  • Understanding jQuery animate function jQuery's .animate() function has helped many front end developers around the world to create web pages with custom animations and effects by just using combinations of […]
  • How to detect keycode / key pressed in jquery Tracking a keyCode can be very helpful if you would like to give your visitors a user friendly experience. For instance, if there is an overlay open on a page, I […]