---
title: "Select previous sibling via jQuery"  
description: "Select previous sibling via jQuery"  
author: "Ethan Karla"  
published: 2021-07-29  
updated: 2023-11-27  
canonical: https://www.mindstick.com/forum/156674/select-previous-sibling-via-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 1 minute  

---

# Select previous sibling via jQuery

How to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) [previous](https://yourviews.mindstick.com/view/81421/unlock-2-0-is-just-like-previous-corona-lockdowns) [sibling via jQuery](https://www.mindstick.com/forum/156678/select-all-next-sibling-via-jquery)?

\

## Replies

### Reply by Aryan Kumar

In jQuery, you can use the **.prev()** method to select the immediately preceding sibling of an element. Here's an example:

```plaintext
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery Select Previous Sibling</title>
  <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
  <style>
    .selected {
      color: red;
    }
  </style>
</head>
<body>

<div class="sibling">First Sibling</div>
<div class="sibling">Second Sibling</div>
<div class="sibling">Third Sibling</div>

<script>
  // Select the second sibling and apply a class for styling
  $(document).ready(function () {
    $('.sibling').eq(1).prev().addClass('selected');
  });
</script>

</body>
</html>
```

In this example:

- The **.sibling** class is applied to each **<div>** element.
- The jQuery code selects the second sibling (using **.eq(1)**, as jQuery uses zero-based indexing) and then selects its previous sibling using **.prev()**.
- The selected previous sibling is given a class (**selected** in this case) for styling.

Make sure to include the jQuery library in your HTML file for this to work. You can adjust the selector and styling based on your specific use case.


---

Original Source: https://www.mindstick.com/forum/156674/select-previous-sibling-via-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
