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.
  • 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.