Javascript удалить атрибут элемент

JavaScript removeAttribute

Summary: in this tutorial, you will learn how to use the JavaScript removeAttribute() to remove the attribute with the specified name from the element.

Introduction to JavaScript removeAttribute() method

The removeAttribute() removes an attribute with a specified name from an element:

element.removeAttribute(name); Code language: CSS (css)

Parameters

The removeAttribute() accepts an argument which is the name of the attribute that you want to remove. If the attribute does not exist, the removeAttribute() method wil not raise an error.

Return value

The removeAttribute() returns a value of undefined .

Usage notes

HTML elements have some attributes which are Boolean attributes. To set false to the Boolean attributes, you cannot simply use the setAttribute() method, but you have to remove the attribute entirely using the removeAttribute() method.

For example, the values of the disabled attributes are true in the following cases:

button disabled>Save Draft button> button disabled="">Save button> button disabled="disabled">Cancel button> Code language: HTML, XML (xml)

Similarly, the values of the following readonly attributes are true :

input type="text" readonly> textarea type="text" readonly=""> textarea type="text" readonly="readonly"> Code language: HTML, XML (xml)

JavaScript removeAttribute() example

The following example uses the removeAttribute() method to remove the target attribute from the link element with the id js :

html> html> head> meta charset="utf-8"> title>JS removeAttribute() Demo title> head> body> a href="https://www.javascripttutorial.net" target="_blank" id="js">JavaScript Tutorial a> script> let link = document.querySelector('#js'); if (link) < link.removeAttribute('target'); > script> body> html> Code language: HTML, XML (xml)
  • Select the link element with id js using the querySelector() method.
  • Remove the target attribute by calling the removeAttribute() on the selected link element.
Читайте также:  Задача про слона питон

Summary

  • Use the removeAttribute() to remove an attribute from a specified element.
  • Setting the value of a Boolean attribute to false will not work; use the removeAttribute() method instead.

Источник

Element: removeAttribute() method

The Element method removeAttribute() removes the attribute with the specified name from the element.

Syntax

Parameters

A string specifying the name of the attribute to remove from the element. If the specified attribute does not exist, removeAttribute() returns without generating an error.

Return value

Usage notes

You should use removeAttribute() instead of setting the attribute value to null either directly or using setAttribute() . Many attributes will not behave as expected if you set them to null .

DOM methods dealing with element’s attributes:

Not namespace-aware, most commonly used methods Namespace-aware variants (DOM Level 2) DOM Level 1 methods for dealing with Attr nodes directly (seldom used) DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used)
setAttribute (DOM 1) setAttributeNS setAttributeNode setAttributeNodeNS
getAttribute (DOM 1) getAttributeNS getAttributeNode getAttributeNodeNS
hasAttribute (DOM 2) hasAttributeNS
removeAttribute (DOM 1) removeAttributeNS removeAttributeNode

Examples

// Given: document.getElementById("div1").removeAttribute("align"); // Now: 

Specifications

Browser compatibility

BCD tables only load in the browser

Found a content problem with this page?

This page was last modified on Apr 7, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Remove the class attribute from an element:

The removeAttribute() method removes an attribute from an element.

The Difference Between removeAttribute() and removeAttributeNode()

The removeAttribute() method removes an attribute, and does not have a return value.

The removeAttributeNode() method removes an Attr object, and returns the removed object.

The result will be the same.

See Also:

Tutorial:

Syntax

Parameters

Return Value

Browser Support

element.removeAttribute() is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью