• 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 Set the Id of a Div

Using jQuery to Set the Id of a Div

March 13, 2022 Leave a Comment

We can use jQuery to set the id of a div by using the jQuery attr() method.

$("#div1 p").attr("id","p1");

Let’s say I have the following HTML:

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

If we want to add an id to the paragraph in the HTML above, we can target that paragraph and then use the attr() method.

$("#div1 p").attr("id","p1");

The resulting HTML would be as follows:

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

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

jQuery("#div1 p").attr("id","p1");

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

We can set 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 no styles. We will then set the id of the paragraph so that it will have different styles. The HTML setup is pretty simple.

Here is the HTML code:

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

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

Below are the two functions that will allow the user to be able to set or remove the id of the paragraph. We can remove an id from an element using the removeAttr() method.

Here is the JavaScript code below:

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

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

Code Output:

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

Set id p1
Remove id p1

Full Code:

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

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Top Position of Element
  • 2.  Using jQuery to Add Required Attribute to Input Field
  • 3.  jQuery appendTo – Insert Element As Last Child
  • 4.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 5.  Using jQuery to Detect Window Resize
  • 6.  Using jQuery to Count Children of an Element
  • 7.  jQuery fadeIn – Fading In Element in HTML
  • 8.  Using jQuery to Get the Margin of an Element
  • 9.  Using jQuery to Swap an Image on Hover
  • 10.  Using jQuery to Get the Border Width of an Element

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