• 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 / 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 Change Text of HTML Element
  • 2.  Using jQuery to Get Element by ID
  • 3.  Using jQuery to Remove Class from an HTML Element
  • 4.  jQuery mouseleave – An Interactive Example of the mouseleave Method
  • 5.  jQuery last() – Get the Last Element
  • 6.  Using jQuery to Add a Sibling to an Element
  • 7.  Using jQuery to Capitalize the First Letter of a String
  • 8.  How to Check if Element Exists Using jQuery
  • 9.  Resizing an Image Using jQuery
  • 10.  Using jQuery to Hide Element by Class

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