Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Working with Attributes | Modifying and Styling the DOM
DOM Manipulation with JavaScript

bookWorking with Attributes

Understanding how to work with attributes is essential for dynamic and interactive web pages. Many elements in HTML have attributes, such as src for images, href for links, and alt for alternative text. JavaScript gives you several ways to read and change these attributes. The two most common methods are getAttribute and setAttribute.

These methods let you access or modify any attribute by name, regardless of whether it is a standard property:

  • Use element.getAttribute("src") to read the src of an image;
  • Use element.setAttribute("alt", "A new description") to update its alt text.

In addition to these methods, some attributes can also be accessed directly as properties—such as element.src or element.href. Direct property access is often more convenient but may behave differently for some attributes, especially when dealing with custom or non-standard attributes.

index.html

index.html

script.js

script.js

copy

In this example, you see both techniques: setAttribute is used to update the src and alt attributes of the image, while the link's href is changed using direct property access. Both approaches achieve similar results for standard attributes, but using the appropriate method is important for consistency and compatibility.

question mark

Which of the following statements about manipulating attributes in JavaScript is correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain the difference between getAttribute/setAttribute and direct property access in more detail?

When should I use setAttribute instead of direct property access?

Are there any attributes that can only be accessed or changed with setAttribute?

Awesome!

Completion rate improved to 6.67

bookWorking with Attributes

Pyyhkäise näyttääksesi valikon

Understanding how to work with attributes is essential for dynamic and interactive web pages. Many elements in HTML have attributes, such as src for images, href for links, and alt for alternative text. JavaScript gives you several ways to read and change these attributes. The two most common methods are getAttribute and setAttribute.

These methods let you access or modify any attribute by name, regardless of whether it is a standard property:

  • Use element.getAttribute("src") to read the src of an image;
  • Use element.setAttribute("alt", "A new description") to update its alt text.

In addition to these methods, some attributes can also be accessed directly as properties—such as element.src or element.href. Direct property access is often more convenient but may behave differently for some attributes, especially when dealing with custom or non-standard attributes.

index.html

index.html

script.js

script.js

copy

In this example, you see both techniques: setAttribute is used to update the src and alt attributes of the image, while the link's href is changed using direct property access. Both approaches achieve similar results for standard attributes, but using the appropriate method is important for consistency and compatibility.

question mark

Which of the following statements about manipulating attributes in JavaScript is correct?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
some-alt