JavaScript DOM getAttribute and setAttribute


Modifying attributes in JavaScript allows you to interact with and change the attributes of HTML elements. The getAttribute and setAttribute methods are commonly used for these purposes.

getAttribute

Definition

  • getAttribute: Retrieves the value of a specified attribute from an element.

Syntax

const value = element.getAttribute(attributeName);

Parameters

  • attributeName: A string representing the name of the attribute whose value you want to retrieve.

Return Value

  • Returns the value of the specified attribute as a string.
  • Returns null if the attribute does not exist.

Usage

  1. Getting an Attribute Value

    <a id="myLink" href="https://example.com" target="_blank">Example</a> <script> const link = document.getElementById('myLink'); const hrefValue = link.getAttribute('href'); console.log(hrefValue); // Logs: 'https://example.com' </script>

    In this example, getAttribute('href') retrieves the value of the href attribute from the <a> element.

  2. Getting a Non-Standard Attribute

    <div id="myDiv" data-info="some data">Content</div> <script> const div = document.getElementById('myDiv'); const dataInfo = div.getAttribute('data-info'); console.log(dataInfo); // Logs: 'some data' </script>

    This retrieves the value of a custom data attribute (data-info).

setAttribute

Definition

  • setAttribute: Sets the value of a specified attribute on an element.

Syntax

element.setAttribute(attributeName, value);

Parameters

  • attributeName: A string representing the name of the attribute to be set.
  • value: A string representing the value to set for the attribute.

Usage

  1. Setting an Attribute Value

    <a id="myLink" href="#">Example</a> <script> const link = document.getElementById('myLink'); link.setAttribute('href', 'https://example.com'); </script>

    This sets the href attribute of the <a> element to https://example.com.

  2. Setting a Non-Standard Attribute

    <div id="myDiv">Content</div> <script> const div = document.getElementById('myDiv'); div.setAttribute('data-info', 'new data'); </script>

    This sets a custom data attribute (data-info) to new data.

Considerations

  • Standard vs. Non-Standard Attributes: getAttribute and setAttribute can be used for both standard HTML attributes (like href, src, class, etc.) and custom attributes (like data-* attributes).

  • Boolean Attributes: For boolean attributes (e.g., checked, disabled, readonly), setAttribute can set the attribute to the string value "true". To remove the attribute, use removeAttribute.

    // Set boolean attribute element.setAttribute('disabled', 'true'); // Remove boolean attribute element.removeAttribute('disabled');
  • Attributes vs. Properties: Attributes and properties are related but distinct. Attributes represent the initial values in HTML, while properties represent the current state of an element. For example, changing the value property of an input element doesn't always change the value attribute. For most cases, setAttribute and getAttribute work well for interacting with attributes.

Best Practices

  • Use setAttribute and getAttribute for Dynamic Attribute Manipulation: These methods are useful when you need to dynamically update attributes or read their values.
  • Prefer Direct Property Access for Standard Attributes: For frequently accessed or manipulated attributes, using properties (like element.href or element.className) can be more straightforward and efficient.