Skip to content
Advertisements

DOM removeAttribute()

As we create HTML elements in JavaScript, we can create and delete their attributes. For this, we use the setAttribute () method to create the attribute, the removeAttribute () method to delete the attribute, the getAttribute () method to find the attribute value, and the hasAttribute () method to check the attribute.


removeAttribute()

removeAttribute () removes an attribute from the specified item.
An exception does not occur when you try to remove an attribute that is not on the element.

Syntax:

element.removeAttribute(name)

name: Enter the name of the feature you want to remove.


removeAttribute Examples

Example 1

removeAttribute example
<body>

    <input class="txt" type="text" value="truecodes.org"/>
    <button onclick="myAttribute();">REMOVE</button>
   
    <script>
        
        function myAttribute() {
            document.getElementsByTagName("INPUT")[0].removeAttribute("class")
        }

    </script>

</body>

Example 2

removeAttribute example
<body>

    <div class="box">
        <input class="txt" type="text" value="box list" />
        <span onclick="removeFunction()"><i class="fas fa-times"></i></span>
    </div>


    <script>

        function removeFunction() {
            document.querySelector(".box").removeAttribute("class");
        }

    </script>

</body>

Advertisements

4 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: