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 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.
- Return the value of an attribute:
- 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.
-
Return the value of an property:
Example 1: This example changes the ID of the element and changes the background color to red by using attr() method.
<!DOCTYPE HTML> <html> <head> <title> Change the element ID </title> <script src = </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> |
chevron_right
filter_none
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.
<!DOCTYPE HTML> <html> <head> <title> Change the element ID </title> <script src = </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> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to change an HTML element name using jQuery ?
- JQuery | Change the text of a span element
- How to change the src attribute of an img element in JavaScript / jQuery ?
- jQuery | change() with Examples
- How to Change the Text of a Button using jQuery?
- How to change cursor style using jQuery ?
- Content editable change event in jQuery
- How to change selected value of a drop-down list using jQuery?
- How to change cursor to waiting state in JavaScript/jQuery ?
- How to remove parent element except its child element using jQuery ?
- Change an element class JavaScript
- JavaScript | Change the text of a span element
- How to change style attribute of an element dynamically using JavaScript ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
- jQuery | Move an element into another element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



