- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove all the attributes of an HTML element using jQuery?
To remove all the attributes of an HTML element, the removeAttr() method won’t work. For this, create an array and use removeAllAttrs() to remove all the attributes of a specific HTML element. For example, remove the image by removing the attributes of the img element.
You can try to run the following code to learn how to remove all the attributes from an element. We’re removing the img element attribute −
Example
<html> <head> <title>Selector Example</title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function() { $(".remove-attr").click(function(){ $.fn.removeAllAttrs= function() { return this.each(function() { $.each(this.attributes, function() { this.ownerElement.removeAttributeNode(this); }); }); }; $('img').removeAllAttrs(); }); }); </script> </head> <body> <button type="button" class="remove-attr">Remove Image</button> <img src="/green/images/logo.png” alt=”MyImage”> </body> </html>
- Related Articles
- How to remove all style attributes using jQuery?
- How to add and remove HTML attributes with jQuery?
- How to remove an element from DOM using jQuery?
- How to set HTML content of an element using jQuery?
- How to get HTML content of an element using jQuery?
- How to remove all the elements from DOM using element ID in jQuery?
- How to add display:none in an HTML element using jQuery?
- Attributes of HTML element
- How to remove all CSS classes using jQuery?
- How to remove all CSS classes using jQuery?
- Remove next element using jQuery?
- How to append an element before an element using jQuery?
- How to append an element after an element using jQuery?
- How can I show and hide an HTML element using jQuery?
- How to get the width of an element using jQuery?

Advertisements