• 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 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.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 2.  jQuery next – Get the Next Element of a Div
  • 3.  Using jQuery to Check if Element Has Class
  • 4.  Toggle the Visibility of an Element Using jQuery
  • 5.  Using jQuery to Change Text of HTML Element
  • 6.  Using jQuery to Set the Width of a Div
  • 7.  jQuery last() – Get the Last Element
  • 8.  Using jQuery to Set a Radio Button to Checked
  • 9.  Changing the Background Image of a div Using jQuery
  • 10.  How to Uncheck All Checkboxes in 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

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