In this tutorial we will see how to Change id of HTML Element in jQuery. The jQuery attr() method can be used to change the id of HTML element.
Table of Contents
jQuery attr() Method
jQuery attr() Method is used to set or get the attributes and values of the selected HTML element
In this example attr() Method is used to change the id of the selected HTML element.
HTML Code
HTML Code is given below, This code has one paragraph element with id named 'old' and a button tag.
<p id="old">Change my id.</p>
<button id="btn">Change id</button>
JQuery Code
JQuery Code is given below, In this code jQuery attr() Method is used to change id of HTML element on click of a button.
Click event is attached with the HTML button using jQuery click() method.
Take a look at the code given below.
<script>
$(document).ready(function(){
$('#btn').click(function(){
$('#old').attr('id' , 'new');
});
});
</script>
The id of paragraph tag will be changed from 'old' to 'new'.
Demo
Video Tutorial
Watch video tutorial on How To Change id of HTML Element with jQuery.