Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

html

What is DOM removeNamedItem() method used for in HTML?

Abdul Muizz

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The removeNamedItem() method dynamically removes attributes of HTML elements as the user only interacts with the web page. This method is used by developers when they want the behavior of their web page to change according to user interaction.

Syntax

The syntax we'll use is as follows:

namednodemap.removeNamedItem(nodename)

Here, namednodemap object is acquired using the attribute property on an HTML element, and contains all attributes associated with that element.

Parameters

This method needs a single parameter which is defined below:

  • nodename: This is a required attribute and a string that specifies the node or the attribute which needs to be removed.

Return value

This method returns the HTML element whose attribute was removed using the removeNamedItem() method.

Example

Let's now make use of this method in the following code example:

Explanation

The explanation for the code in the HTML tab is given below:

  • Line 6: Here, we have an <input> tag with value and class attributes. We fetch this tag to demonstrate the functionality of removeNamedItem(). To read more on the value attribute, please refer to this Answer.
  • Line 7: In this line of code, we have a <button> tag, which listens for the onclick event and triggers the removeAttr() method as the user clicks on the <button>.
  • Lines 9–14: This piece of code contains the <script> tag, which encapsulates our removeAttr() method with the following lines of code:
    • Line 11: First, we use the document.getElementById() method to get hold of our <input> tag and use the attribute property to store all of its attributes in namenodemap.
    • Line 12: Lastly, we use the removeNamedItem() method to remove the class attribute from the <input> tag—removing all the styles applied.

Note: For more details on the document.getElementById() method, please visit this link.



RELATED TAGS

html

CONTRIBUTOR

Abdul Muizz
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring