How to Change the ID of Element using JavaScript? Last Updated : 19 Sep, 2024 Comments Improve Suggest changes Like Article Like Report We are given an element and the task is to change the ID of elements using JavaScript. ID is unique for any element and it can be assigned to an element only once. JavaScript provides a method to access this id and also to manipulate the id. Syntax:Selected_element.id = newID;Below are the appraoches to change the ID of element using JavaScript:Table of ContentUsing the id propertyUsing the id property with inline onclick functionUsing the id propertyIn this approach we are using the id property in JavaScript to change the ID of an HTML element. The id property allows us to get or set the ID of an element.Example: In this example, we will use the id property for changing the id. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to Change the ID of an Element Using JavaScript</title> <style> .div { height: 100px; width: 200px; margin: 20px auto; color: white; transition: background-color 0.3s; } #div1 { background: green; } #div2 { background: blue; } body { font-family: Arial, sans-serif; text-align: center; margin: 0; padding: 20px; } h1 { color: green; } button { padding: 10px 20px; border: none; border-radius: 5px; background-color: #3498db; color: white; font-size: 16px; cursor: pointer; transition: background-color 0.3s; } button:hover { background-color: #2980b9; } </style> </head> <body> <h1>GeeksForGeeks</h1> <p id="GFG_UP"></p> <div class="div" id="div1"></div> <br> <button onclick="GFG_Fun()"> Click Here </button> <script> let el_up = document.getElementById('GFG_UP'); el_up.innerHTML = "Click on the button to change the ID of the box."; function GFG_Fun() { let box = document.getElementById('div1'); if (box) { box.id = 'div2'; // Change the ID of the div } } </script> </body> </html> Output: Using the id property with inline onclick functionUsing JavaScript, we can use the id property inside the element to change the ID.Example: In this example, we will use the id property for changing the id. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> How to change the ID of element using JavaScript ? </title> <style> .div { height: 100px; width: 200px; margin: 0 auto; color: white; } #div1 { background: green; } #div2 { background: blue; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <div class="div" id="div1"></div> <br> <button onclick="document.getElementById( 'div1').id = 'div2'; return false"> click here </button> <script> let el_up = document.getElementById('GFG_UP'); el_up.innerHTML = "Click on button to" + " change the ID of box."; </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Change the ID of Element using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to change the element id using jQuery ? The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to 3 min read How to get all ID of the DOM elements with JavaScript ? Given a HTML document and the task is to get the all ID of the DOM elements in an array. There are two methods to solve this problem which are discusses below: Approach 1: First select all elements using $('*') selector, which selects every element of the document.Use .each() method to traverse all 2 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read How to Add an ID to Element in JavaScript ? In JavaScript, the ID is the unique identifier through which the elements can be identified and manipulated if required. We can add an ID to an element in JavaScript using various approaches that are explored in this article with practical demonstration. Below are the possible approaches: Table of C 2 min read How to Select an Element by ID in JavaScript ? In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic 2 min read Like