VOOZH about

URL: https://www.geeksforgeeks.org/html/html-dom-setattribute-method/

⇱ HTML DOM setAttribute() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML DOM setAttribute() Method

Last Updated : 23 Jul, 2025

The HTML DOM setAttribute() method is used to get the value of the attribute of the element. By specifying the name of the attribute, it can set the value of that element. If the element is already present then its value is updated.

Syntax:

Object.setAttribute(attributename,value)

Parameters: This method accepts two parameters:

  • attributename: It is a string whose value is to be set.
  • value: It is a string value that is to be assigned to the attributename.

Return Values: This parameter does not return any values.

The below examples show the use of the setAttribute() method.

Example 1: In this example, we will add a href attribute to the anchor tag using the setAttribute() method.

Output:

👁 HTML DOM setAttribute() Method
HTML DOM setAttribute() Method

Example 2: In this example, we will change the type of an input tag to a button using the setAttribute() method.

Output:

👁 HTML DOM setAttribute() Method
HTML DOM setAttribute() Method

We have a complete list of HTML DOM methods, to check those please go through this article.

Supported Browsers: The browser supported by DOM setAttribute()method are listed below: 

  • Google Chrome 1.0
  • Internet Explorer 5.0
  • Microsoft Edge 12.0
  • Firefox 1.0
  • Opera 8.0
  • Safari 1.0
Comment
Article Tags:
Article Tags: