Skip to content

Initial commit, description and example of setAttribute() #6280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
Title: '.setAttribute()'
Description: 'Sets the attribute of a specified element.'
Subjects:
- 'Web Development'
- 'Web Design'
Tags:
- 'Attributes'
- 'DOM'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

The **`.setAttribute()`** method of the `Element` interface sets or updates an attribute of the specified element. If the given attribute already exists, its value is updated; otherwise, a new attribute is created with the given name and value.

## Syntax

```pseudo
elm.setAttribute(name, value);
```

- `elm`: The element on which the `.setAttribute()` method is called.
- `name` (string): The name of the attribute to set (e.g., `"class"`, `"id"`, `"href"`).
- `value` (string): The value to assign to the specified attribute.

## Example

This example sets the `"name"` attribute of a button element to `"helloButton"`:

```js
const button = document.querySelector('button');

button.setAttribute('name', 'helloButton');
```