Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with Element Attributes | DOM Manipulation
Advanced JavaScript Mastery
course content

Conteúdo do Curso

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

book
Working with Element Attributes

Attributes are values added to HTML elements to provide additional information or modify their behavior. JavaScript provides several methods to work with these attributes, allowing you to dynamically get, set, remove, or check for specific attributes.

GetAttribute()

getAttribute() is used to retrieve the value of a specified attribute from an element. It's useful when you need to access specific attribute values like href, src, class, etc.

html

index

css

index

js

index

copy

Accesses the value of the href attribute of the anchor (<a>) element.

SetAttribute()

setAttribute() is used to add a new attribute or change the value of an existing attribute on an element.

html

index

css

index

js

index

copy

Sets or updates the alt attribute of the image, allowing for dynamic changes based on user actions or application logic.

RemoveAttribute()

removeAttribute() removes a specified attribute from an element, making it useful for conditionally toggling attributes based on certain events or states.

html

index

css

index

js

index

copy

Removes the disabled attribute, enabling the button for interaction.

HasAttribute()

hasAttribute() checks whether an element has a specified attribute. This method is particularly useful for conditional logic, ensuring that certain attributes exist before performing further actions.

html

index

css

index

js

index

copy

Checks if the required attribute exists on the input field and logs a message accordingly.

Differences Between Properties and Attributes

While properties and attributes are often used interchangeably, they serve different roles in the DOM. Let's inspect their differences.

html

index

css

index

js

index

copy
  • The value attribute retains the original value defined in the HTML;
  • The value property reflects the current, dynamically updated state of the input.

Practical Example: Managing Product Details on an E-commerce Site

Imagine you have a product details section where users can update product information such as availability, featured status, and shipping options. This example demonstrates how attributes are used to dynamically control these aspects.

html

index

css

index

js

index

copy

The setAttribute() method is used to dynamically add specific attributes to elements based on user actions. For instance, when the availability status is toggled to "Out of Stock," setAttribute() adds an out-of-stock class to apply unique styling. Similarly, it adds a data-featured attribute to the featured checkbox when the product is marked as featured.

The removeAttribute() method removes these attributes when they are no longer needed. For example, it removes the out-of-stock class when toggling back to "In Stock" and removes the data-featured attribute if the product is no longer marked as featured. Additionally, it uses removeAttribute() to toggle the visibility of the shipping information by adding or removing the hidden attribute.

Lastly, hasAttribute() checks for the presence of these attributes, such as verifying if the data-featured attribute exists before updating the featured status or checking the hidden attribute on shipping info to determine its visibility state.

1. Which method is used to retrieve the value of an attribute from an element?
2. What does the `setAttribute()` method do?
3. What will the following code output?
Which method is used to retrieve the value of an attribute from an element?

Which method is used to retrieve the value of an attribute from an element?

Selecione a resposta correta

What does the `setAttribute()` method do?

What does the setAttribute() method do?

Selecione a resposta correta

What will the following code output?

What will the following code output?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 7
We're sorry to hear that something went wrong. What happened?
some-alt