Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
PHP | DOMElement setAttribute() Function
Next article icon

HTML DOM setAttribute() Method

Last Updated : 07 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

HTML
<!DOCTYPE html> <html> <body>     <h1 style="color:green">         Geeks for Geeks     </h1>     <h2>setAttribute() Method</h2>      <a id="geek">Click to go to geeksforgeeks.org</a>      <p>         Click the button below to add an href         attribute to the element above.     </p>      <button onclick="myFunction()">Click Me</button>      <script>         function myFunction() {           document.getElementById("geek").setAttribute           ("href", "https://www.geeksforgeeks.org");          }     </script> </body> </html> 

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.

HTML
<!DOCTYPE html> <html> <body>     <h1 style="color:green">         Geeks for Geeks     </h1>     <h2>setAttribute() Method</h2>      <input id="geeks" value="Hello Geek">      <p>         Click the button below to change the         input field to an input button.     </p>     <button onclick="changeType()">Change</button>      <script>         function changeType() {           document.getElementById("geeks").setAttribute("type", "button");          }     </script> </body> </html> 

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 HTML DOM Object Complete reference 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

Next Article
PHP | DOMElement setAttribute() Function

A

amitsingh2730
Improve
Article Tags :
  • Web Technologies
  • HTML

Similar Reads

  • HTML DOM setAttributeNode() Method
    The setAttributeNode() method in HTML DOM is used to add the specified attribute node to an element. If the specified attribute is already present, then this method replaces it. Syntax: element.setAttributeNode(name) Parameter: Only one parameter is accepted name.Where the name is the attribute node
    1 min read
  • HTML DOM removeAttribute() Method
    The DOM removeAttribute() method is used to remove an attribute with specified name from the element. It is similar to the removeAttributeNode() method but the difference is that the removeAttributeNode method is used to remove the specified attribute object, but on the other hand, removeAttribute r
    1 min read
  • HTML DOM removeAttributeNode() Method
    The DOM removeAttributeNode() method is used to remove the specified attribute from the current element. It is similar to removeAttribute() method but the difference is that the removeAttribute method is used to remove the attribute with the specified name, but on the other hand removeAttributeNode
    1 min read
  • HTML DOM setNamedItem() Method
    The setNamedItem() method is used to add a particular node to an attribute node using its name. These attribute nodes are collectively called as namedNodeMap. It can be accessed through a name. If a node is already present in the document, it will replace it and return the updated value. The setName
    2 min read
  • PHP | DOMElement setAttribute() Function
    The DOMElement::setAttribute() function is an inbuilt function in PHP which is used to set an attribute with given name to the given value. If the attribute does not exist, it will be created. Syntax: DOMAttr DOMElement::setAttribute( string $name, string $value ) Parameters: This function accepts t
    2 min read
  • HTML | DOM Textarea Object
    The Textarea Object in HTML DOM is used to represent the HTML <textarea> Element. The textarea element can be accessed by using getElementById() method.Syntax: document.getElementById("ID"); Where ID is assigned to the <textarea> element.Property Values: autofocus: It is used to set or r
    3 min read
  • PHP | DOMElement setAttributeNS() Function
    The DOMElement::setAttributeNS() function is an inbuilt function in PHP which is used to set an attribute with given namespace and name to the given value. If the attribute does not exist, it will be created. Syntax: void DOMElement::setAttributeNS( string $namespaceURI, string $qualifiedName, strin
    2 min read
  • PHP | DOMElement setIdAttribute() Function
    The DOMElement::setIdAttribute() function is an inbuilt function in PHP which is used to declare the attribute specified by name to be of type ID. Syntax: void DOMElement::setIdAttribute( string $name, bool $isId ) Parameters: This function accepts two parameters as mentioned above and described bel
    2 min read
  • PHP | DOMElement setAttributeNode() Function
    The DOMElement::setAttributeNode() function is an inbuilt function in PHP which is used to add a new attribute node to element. Syntax: DOMAttr DOMElement::setAttributeNode( DOMAttr $attr ) Parameters: This function accepts a single parameter $attr which holds the attribute node to be added. Return
    2 min read
  • PHP | DOMElement setIdAttributeNS() Function
    The DOMElement::setIdAttributeNS() function is an inbuilt function in PHP which is used to declare the attribute specified by given local name and namespace URI to be of type ID. Syntax: void DOMElement::setIdAttributeNS( string $namespaceURI, string $localName, bool $isId ) Parameters: This functio
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences