• 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 Add id to div

Using jQuery to Add id to div

January 16, 2022 Leave a Comment

To add an id to a HTML div using jQuery, the simplest way is to use the attr() method.

$("div").attr("id", "div1");

Let’s say I have the following HTML:

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

If we want to add the id “div1” to the element with class “class1”, we can use the jQuery attr() method to do this with the following Javascript code.

$(".class1").attr("id", "div1");

The resulting HTML would be as follows:

<div id="div1" class="class1">
  <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(".class1").attr("id", "div1");

Adding an Id to an HTML Element Using jQuery With a Click

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

Let’s say we have the following HTML code and we want to give the user the ability to add an id to the paragraph. The new id will underline the paragraph content.

<style>#p-new { text-decoration:underline; } </style>
<div id="div1">
  <p id="click-me">Click me to add an id to the paragraph below</p>
  <p class="p">This is the paragraph we will add an id to.</p>
</div>

We can utilize both the jQuery click() method and jQuery attr() method to add an id to the paragraph and make the contents underlined.

Below is the Javascript code which will allow the user to be able to add an id to the paragraph:

$("#click-me").click(function(){
    $(".p").attr("id", "p-new");
}); 

The final code and output for this example of how to add an id using jQuery and Javascript is below:

Code Output:

Click me to add an id to the paragraph below

This is the paragraph we will add an id to.

Full Code:

<style>#p-new { text-decoration:underline; } </style>
<div class="html-code-output">
<div id="div1">
  <p id="click-me">Click me to add an id to the paragraph below</p>
  <p class="p">This is the paragraph we will add an id to.</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $(".p").attr("id", "p-new"); // this will add the id "p-new" to the paragraph with class "p"
});

</script>

Hopefully this article has been useful for you to understand how to add id to a div and HTML element using jQuery.

Other Articles You'll Also Like:

  • 1.  Input Mask Phone Number Using jQuery
  • 2.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 3.  Using jQuery to Get the Bottom Position of Element
  • 4.  Using jQuery to Delete an Element
  • 5.  Using jQuery to Get the Border Width of an Element
  • 6.  jQuery fadeOut – Fading Out Element in HTML
  • 7.  Using jQuery to Convert a String to Uppercase
  • 8.  jQuery Compare Dates
  • 9.  jQuery contains – How to Check if a Paragraph Contains Specific Text
  • 10.  Using jQuery to Check if Checkbox is Checked

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