How to remove all the attributes of an HTML element using jQuery?

jQueryWeb DevelopmentFront End Technology

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

Live Demo

<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>
raja
Published on 14-Dec-2017 08:46:22
Advertisements