Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6185189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:40:09+00:00 2026-05-24T01:40:09+00:00

Here’s the problem ( live copy ): CSS: div { border: 1px solid black;

  • 0

Here’s the problem (live copy ):

CSS:

div {
    border: 1px solid black;
}

HTML:

<div>.</div><br>
<div>.</div><br>
<div class="test">Should have a blue border</div><br>
<div>.</div><br>
<div>.</div><br>
<div>.</div><br>
<div>.</div><br>
<div>.</div><br>
<div>.</div><br>
<div>.</div><br>

JavaScript:

$('div:last').css('border', '1px solid red')
    .prev('.test').css('border', '1px solid blue');

It seems that when I do $(selector).prev(.className) or $(selector).prev(tag.className) I get an empty set. What’s wrong with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:40:10+00:00Added an answer on May 24, 2026 at 1:40 am

    prev only looks back one sibling, and returns either a set containing that sibling if it’s a match for the selector, or an empty set if it isn’t a match. You want prevAll, which searches backward through the siblings looking for matches, possibly combined with first or :first.

    Like this (live copy):

    $('div:last').css('border', '1px solid red')
        .prevAll('.test').first().css('border', '1px solid blue');
    

    There I’ve used first (the function) rather than :first (the selector). Counter-intuitively (to me), it’s faster, since it can hand the prevAll part off to native code in most browsers (more in this other SO question and answer), but only if we don’t use jQuery-specific extensions like :first.

    To understand why prev is defined the way it is, you have to remember how set-based jQuery is. Looking at an entire set of elements, prev returns a new set of the immediate sibling of each of those elements provided it matches the given selector. So for instance, consider this (live copy):

    CSS:

    div {
      border: 1px solid black;
      margin-bottom: 2px;
    }
    

    HTML:

    <div class="foo">foo</div>
    <div class="bar">bar</div>
    <div class="foo">foo</div>
    <div class="bar">bar</div>
    <div>not foo</div>
    <div class="bar">bar</div>
    <div class="foo">foo</div>
    <div class="bar">bar</div>
    <div>not foo</div>
    <div class="bar">bar</div>
    <div class="foo">foo</div>
    <div class="bar">bar</div>
    

    JavaScript:

    jQuery(function($) {
    
      $("div.bar")
        .css("background-color", "#ffffcc")
        .prev(".foo")
        .css("background-color", "#ccffcc");
    
    });
    

    The initial $("div.bar") matches six elements in different locations in the page. We set their background colors, and then get their previous siblings if those siblings have the class “foo”; our new set only has four elements, because only four of the immediate previous siblings had that class. Then we set their backgrounds to green.

    prev seems largely useless when you’re dealing with individual elements, but its value makes sense when you look at things from a set perspective. (jQuery is so set-based that for ages I assumed the name “jQuery” was because it’s like SQL [the Structured Query Language], which is also massively set-based; but Resig says that’s not the case.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my button element: <button type=submit class=btn-submit><span><strong>Let Me In, please!</strong></span></button> and here's the CSS:
Here is the test code that I am using : public class IOConnectDirect extends
Here's my HTML code. <table cellspacing=0 class=assets_table> <tbody> <tr class=table_header><td>ID</td><td>Title</td><td>Regarding</td><td>Status</td><td>Date</td></tr> <tr><td>9</td><td>Testing</td><td>hijacked account</td><td></td><td> 4 Jan,
Here's a problem I ran into recently. I have attributes strings of the form
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here is my current problem. I am working with the chat module and I'm
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is what I want to do. Use this HTML line and have the
Here is my problem: My Spring MVC website is going to rely a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.