• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / jQuery / Using jQuery to Change the Id of a Div

Using jQuery to Change the Id of a Div

March 13, 2022 Leave a Comment

We can use jQuery to change the id of a div by using the jQuery attr() method. To do this we can target the current id of the div, and then use the attr() method to then change the id.

$("#div1").attr("id","div2");

Let’s say I have the following HTML:

<div id="div1">
  <p class="p">This is a paragraph.</p>
</div>

If we want to change the div id from “div1” to “div2” we could use the jQuery attr() method to do this with the following JavaScript code.

$("#div1").attr("id","div2");

The resulting HTML would be as follows:

<div id="div2">
  <p class="p">This is a paragraph.</p>
</div>

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").attr("id","div2");

Using jQuery to Change the Id of a Div With a Click

We can change the id of an HTML element using jQuery very easily by combining the attr() method with a click event.

In this example, we will have a paragraph with an id that has certain styles. We will replace that id with another id that has different styles. The HTML setup is pretty simple.

Here is the HTML code:

<style>#p1 { text-decoration:underline; } #p2 { font-weight: bold; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can change the id of.</p>
  <div id="click-me1" class="click-me">Change to id p2</div>
  <div id="click-me2" class="click-me">Change to id p1</div>
</div>

We can utilize both the jQuery click() method and the jQuery attr() method to change the id of the paragraph to either #p1 or #p2. If the paragraph has id “p1”, then it will have an underline style. If it has id “p2”, then it will be bold.

Below are the two functions that will allow the user to be able to change the id of the paragraph:

$("#click-me1").click(function(){
  $("#p1").attr("id","p2");
}); 
$("#click-me2").click(function(){
  $("#p2").attr("id","p1");
}); 

The final code and output for this example of using jQuery to change the id of a div with a click is below:

Code Output:

This is the paragraph that we can change the id of.

Change to id p2
Change to id p1

Full Code:

<style>#p1 { text-decoration:underline; } #p2 { font-weight: bold; }</style>
<div id="div1">
  <p id="p1">This is the paragraph that we can change the id of.</p>
  <div id="click-me1" class="click-me">Change to id p2</div>
  <div id="click-me2" class="click-me">Change to id p1</div>
</div>

<script>

$("#click-me1").click(function(){
  $("#p1").attr("id","p2");
}); 
$("#click-me2").click(function(){
  $("#p2").attr("id","p1");
}); 

</script>

Hopefully this article has been useful for you to understand how to use jQuery to change the id of a div.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Remove the Id of a Div
  • 2.  Using jQuery to Append Text to textarea Field
  • 3.  How to Check if Element Exists Using jQuery
  • 4.  Using jQuery to Get the Top Position of Element
  • 5.  jQuery hover – An Interactive Example of the hover Method
  • 6.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 7.  Using jQuery to Change Label Text
  • 8.  Using jQuery to Show a Div
  • 9.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 10.  Examples of Text Animations Using jQuery

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy