How to change the element id using jQuery ? Last Updated : 24 May, 2019 Comments Improve Suggest changes Like Article Like Report 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 set attribute values, it sets one or more than one attribute/value pairs for the set of selected elements. Syntax: Return the value of an attribute: $(selector).attr(attribute) Set the attribute and value: $(selector).attr(attribute, value) Set attribute and value by using a function: $(selector).attr(attribute, function(index, currentvalue)) Set multiple attributes and values: $(selector).attr({attribute:value, attribute:value, ...}) Parameters: attribute: This parameter specifies the name of the attribute. value: This parameter specifies the value of the attribute. function(index, currentvalue): This parameter specifies a function that returns the attribute value to set. index: This parameter receives the index position of element in the set. currentValue: This parameter receives the current attribute value of selected elements. jQuery prop() Method: This method set/return properties and values of the matched elements. If this method is used to return the property value, it returns the value of the first selected element. If this method is used to set property values, it sets one or more property/value pairs for the set of selected elements. Syntax: Return the value of an property: $(selector).prop(property) Set the property and value: $(selector).prop(property, value) Set property and value using a function: $(selector).prop(property, function(index, currentvalue)) Set multiple properties and values: $(selector).prop({property:value, property:value, ...}) Parameters: property: This parameter specifies the name of the property. value: This parameter specifies the value of the property. function(index, currentvalue): This parameter specifies a function that returns the property value to set. index: This parameter receives the index position of element in the set. currentValue: This parameter receives the current property value of selected elements. Example 1: This example changes the ID of the element and changes the background color to red by using attr() method. html <!DOCTYPE HTML> <html> <head> <title> Change the element ID </title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> #myCol { background: green; } #newID { background: red; } table { color: white; } td { padding: 10px; } </style> </head> <body> <center> <h1 style = "color:green;" > GeeksForGeeks </h1> <table> <colgroup> <col id= "myCol" span= "3"> <col style= "background-color:green"> </colgroup> <tr> <th>S.No</th> <th>Title</th> <th>Geek_id</th> </tr> <tr id = "row1"> <td>G_1</td> <td>GFG</td> <th>Geek_id_1</th> </tr> <tr> <td>Geek_2</td> <td>GeeksForGeeks</td> <th>Geek_id_2</th> </tr> </table> <br> <button onclick = "Geeks()"> Click here </button> <script> function Geeks() { $("col").attr('id', 'newID'); } </script> </center> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: This example changes the ID of the element and changes the background color to red by using prop() method. html <!DOCTYPE HTML> <html> <head> <title> Change the element ID </title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <style> #myCol { background: green; } #newID { background: red; } table { color: white; } td { padding: 10px; } </style> </head> <body> <center> <h1 style = "color:green;" > GeeksForGeeks </h1> <table> <colgroup> <col id= "myCol" span= "3"> <col style= "background-color:green"> </colgroup> <tr> <th>S.No</th> <th>Title</th> <th>Geek_id</th> </tr> <tr id = "row1"> <td>G_1</td> <td>GFG</td> <th>Geek_id_1</th> </tr> <tr> <td>Geek_2</td> <td>GeeksForGeeks</td> <th>Geek_id_2</th> </tr> </table> <br> <button onclick = "Geeks()"> Click here </button> <script> function Geeks() { $("col").prop('id', 'newID'); } </script> </center> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to change the element id using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies jQuery-Misc Similar Reads How to change an HTML element name using jQuery ? Given an HTML document and the task is to replace an HTML element by another element. For example: we can change an element <b> with <h1> element without changing any other property.Approach: Select the HTML element which need to be change.Copy all attributes of previous element in an ob 2 min read How to Change the ID of Element using JavaScript? 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 appraoche 2 min read How to run the code on change event using jQuery ? In this article, we will see how to run the code on change events using jQuery. The change event is used to trigger when the user changes the value of the element. Syntax: $(selector).change(function) Example: In the below example, we are creating a textarea element that contains GeeksforGeeks text. 1 min read How to change the color of an icon using jQuery ? In this article, we will see how to change the color of the icon using jQuery. To change the color of the icon, we will use a jquery method. jQuery css() method this method is used to change the styling of a particular selector. This method is also can be used for changing the color of the icon. Fir 2 min read How to get the outer html of an element using jQuery ? Sometimes, there is a need to get the entire HTML element by its id and not merely its contents, for doing so, we shall use the HTML DOM outerHTML Property to get the outer HTML of HTML element. Syntax: document.getElementById("your-element-id").outerHTML) You can use a variable and initialize it to 2 min read Like