• 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
  • Ruby
  • 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.  Changing the Background Image of a div Using jQuery
  • 2.  jQuery get img src – Get the Source of an Image Using jQuery
  • 3.  How to Use jQuery to Clear a textarea Field
  • 4.  Using jQuery to Get the Next Sibling of an Element
  • 5.  jQuery insertBefore – Insert Element Before Another Element
  • 6.  Using jQuery to Change Text of HTML Element
  • 7.  How to Clear an Input Field Using jQuery
  • 8.  jQuery width() – Using jQuery to Get the Width of a Div
  • 9.  Get Padding of an Element Using jQuery
  • 10.  Using jQuery to Select Multiple Classes

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy