Open In App

How to change the element id using jQuery ?

The jQuery methods are used to change the element ID which are described below:

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>                    

Output:

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>                    

Output:


Article Tags :