
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove All 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="/https/www.tutorialspoint.com/green/images/logo.png” alt=”MyImage”> </body> </html>
Advertisements