• 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 Get Element by ID

Using jQuery to Get Element by ID

December 2, 2021 Leave a Comment

To get an element by id using jQuery, the simplest way is using the jQuery id selector.

$("#id-of-element")

Let’s say I have the following HTML:

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

To get the div element by id using jQuery, we just will use the jQuery id selector with the following Javascript code. This allows us to get a specific HTML element by the ID attribute.

$("#div")

If the element exists in the DOM, then $(“#div”) will return a jQuery object which we can operate on. If the element does not exist, $(“#div”) will be empty.

If the element exists, we can then do something like add a style to the element:

$("#div").css("text-align","center");

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

jQuery("#div").css("text-align","center");

Get Element By ID On Click Using jQuery

Many times when creating a web page and the user experience, we want to do things when a user interacts with another element on the web page.

We can get an element by ID after a click using jQuery very easily by combining the jQuery ID selector and text() methods with a click event.

Let’s say we have the following HTML code and we want to select the div and change the background color.

<div id="div">
    <p id="click-me">Click Me to Select Div and Change Background Color</p>
</div>

We can utilize the jQuery click() method and the jQuery ID selector to get both the click event and to select the div.

Below is the Javascript code which will allow the us to get the div element by ID using jQuery:

$("#click-me").click(function(){
    $("#div").css("background-color","green");
}); 

The final code and output for this example of how to get an element by ID on click using jQuery and Javascript is below:

Code Output:

Click Me to Select Div and Change Background Color

Full Code:


<div class="html-code-output">
<div id="div">
    <p id="click-me">Click Me to Select Div and Change Background Color</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#div").css("background-color","green");
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to get an element by ID.

Other Articles You'll Also Like:

  • 1.  How to Use jQuery to Check if an Element is Visible
  • 2.  Get nth Child with jQuery
  • 3.  jQuery siblings – Get the Sibling Elements of a Div
  • 4.  Using jQuery to Get the Current URL
  • 5.  Using jQuery to Get the Top Position of Element
  • 6.  How to Use jQuery to Animate the Font Size of a Paragraph
  • 7.  Using jQuery to Add id to div
  • 8.  jQuery Display None
  • 9.  Using jQuery to Remove Background Color
  • 10.  How to Set Checkbox Checked With 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