X



Home>Tutorial>

JavaScript removeAttribute() Method and Examples

Nuelson Penuel Wednesday, August 2, 2023 Javascript

JavaScript removeAttribute() Method

The `removeAttribute()` method in JavaScript is used to remove a specified attribute from an HTML element. It allows you to remove any attribute, such as "class", "src", "href", "style", or custom attributes, from an element.

JavaScript removeAttribute() Syntax

The syntax of the `removeAttribute()` method is as follows:
element.removeAttribute(attributeName)
Here, `element` is the HTML element on which you want to remove the attribute, and `attributeName` is the name of the attribute you want to remove. For example, let's say you have an HTML image element with the id "myImage" and want to remove the "src" attribute from it using JavaScript. You can use the `removeAttribute()` method as shown below:
let imgElement = document.getElementById("myImage");
imgElement.removeAttribute("src");
After executing the code above, the "src" attribute will be removed from the "myImage" element.

More Examples of JavaScript removeAttribute()

Sure! Here are a few more examples of using the `removeAttribute()` method: 1. Removing a class attribute from an element:
let element = document.getElementById("myElement");
element.removeAttribute("class");
In this example, the `class` attribute of the element with id "myElement" will be removed. 2. Removing multiple attributes:
let element = document.getElementById("myElement");
element.removeAttribute("src");
element.removeAttribute("alt");
Here, the `src` and `alt` attributes of the element with id "myElement" will be removed. 3. Removing a custom data attribute: HTML
<div id="myDiv" data-custom-attribute="value"></div>
JavaScript
let element = document.getElementById("myDiv");
element.removeAttribute("data-custom-attribute");
In this case, the `data-custom-attribute` will be removed from the div element with id "myDiv". 4. Removing inline styling: HTML
<div id="myDiv" style="color: red;"></div>
JavaScript
let element = document.getElementById("myDiv");
element.removeAttribute("style");
Here, the `style` attribute, which contains the inline CSS styling, will be removed from the div element with id "myDiv". These examples demonstrate different use cases of the `removeAttribute()` method to remove different attributes from HTML elements using JavaScript.

Conclusiom

It's important to note that when you remove an attribute using `removeAttribute()`, the element reverts to the default behavior associated with that attribute. For example, if you remove the "src" attribute from an image element, the image will no longer be displayed. Additionally, if you try to remove an attribute that does not exist on the element, the `removeAttribute()` method will simply do nothing. Overall, the `removeAttribute()` method provides a way to dynamically manipulate HTML attributes in JavaScript by removing them from elements.

| | Views(278)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services