• 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 / jQuery first() – Get the First Element

jQuery first() – Get the First Element

February 3, 2022 Leave a Comment

With the jQuery first() method, we can get the first element or div in a series of elements or divs.

$("div").first();

Let’s say we have the following HTML:

<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>

As we can see, we have three divs with class “divs”, each of which has two paragraphs. If we want to get the first div with class “divs” and change only its background color, we can do this using the jQuery first() method.

Here is the JavaScript code that would accomplish this:

$(".divs").first().css("background","green");

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

jQuery(".divs").first().css("background","green");

Using jQuery to Get the First Div and Change its Background

In this example, we will get the first div of a series of divs with the same class using the jQuery first() method, and change its background color.

Here is our HTML setup with some styles added to the divs:

<style>.divs { border: 1px solid #444; margin-bottom: 20px; padding: 10px; }</style>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div id="click-me">Change background</div>

We can utilize the jQuery first() method, and the jQuery css() method to change the background of the first div.

Below is the Javascript code which will allow the user to be able to change the first div’s background color using jQuery.

$("#click-me").click(function(){
  $(".divs").first().css("background","#c1e9c1");
});

The final code and output for this example is below:

Code Output:

This is some text

This is some text

This is some text

This is some text

This is some text

This is some text

Change background

Full Code:

<style>.divs { border: 1px solid #444; margin-bottom: 20px; padding: 10px; }</style>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div class="divs">
  <p>This is some text</p>
  <p>This is some text</p>
</div>
<div id="click-me">Change background</div>

<script>

$("#click-me").click(function(){
  $(".divs").first().css("background","#c1e9c1");
});

</script>

Hopefully this article has been useful to help you understand the jQuery first() method.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Toggle Class of HTML Element
  • 2.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery
  • 3.  jQuery val Method – Get the Value from an Input Field
  • 4.  jQuery siblings – Get the Sibling Elements of a Div
  • 5.  Using jQuery to Add a Sibling to an Element
  • 6.  jQuery parent – Get the Parent Element of a Div
  • 7.  Using jQuery to Hide Element by Class
  • 8.  Using jQuery to Set the Width of a Div
  • 9.  Using jQuery to Change Label Text
  • 10.  Using jQuery to get Textarea Value

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