Search⌘ K
AI Features

Modifying Element Attributes

Explore how to use JavaScript methods like getAttributeNames, getAttribute, setAttribute, and removeAttribute to modify HTML element attributes. This lesson helps you enhance web page interactivity by dynamically changing element properties within the DOM.

Element.getAttributeNames()

Use the Element.getAttributeNames() function to get an array containing the names of the HTML element’s attributes.

Console
Implementation of Element.getAttributeNames()

Element.getAttribute(name)

You can access the value of a specific attribute within an HTML element using Element.getAttribute() passing the attribute’s name as a string argument.

Console
Implementation of Element.getAttribute(name)

Element.setAttribute(name, value) + Element.removeAttribute(name)

You can change the value of an attribute using the setAttribute() function. Additionally, you can remove attributes using the removeAttribute() function.

Console
Implementation of Element.setAttribute(name, value) and Element.removeAttribute(name)

Exercise

Modify the following ordered list programmatically to use big Roman numerals for the list items.

Test your learning