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 7807219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:47:07+00:00 2026-06-02T02:47:07+00:00

I have some divs in my code and I want to loop them checking

  • 0

I have some divs in my code and I want to loop them checking the actual with the last one.

Why this is not working?

<div class="tarefa">1</div>
<div class="tarefa">2</div>
<div class="tarefa">3</div>

<script>
    $(function() {

        i = 0 ;  
        $('.tarefa').each(function(index) {
            i++;
            if($(this).css("left") == $(this).prev('.tarefa').css("left")){
                alert(i);
            }
        });
    });
</script>

This $(this).prev('.tarefa').css("left") always get undefined.

  • 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-06-02T02:47:10+00:00Added an answer on June 2, 2026 at 2:47 am

    If you had included your HTML, then we could have answered much more directly. I see two potential issues with your code.

    1) When comparing the first item in the .each(), there can be no .prev() so you are asking for .css("left") on an empty jQuery object. That will be undefined at best which will probably fail the equality test, but isn’t generally a good idea.

    2) If all your .tarefa objects are not immediate siblings with no intervening objects, then $(this).prev('.tarefa') is not doing what you expect.

    If you check the jQuery doc for .prev() it gets the previous sibling only if it matches that class. It doesn’t get the previous item in your jQuery object so if your .tarefa objects are not immediate siblings of each other, your code will not work as desired.

    You can fix your code against both issues with this version:

    $(function() {
    
        i = 0 ;  
        var items = $('.tarefa');
        items.each(function(index) {
            i++;
            if(index > 0 && $(this).css("left") == items.eq(index-1).css("left")){
                alert(i);
            }
        });
    });
    

    EDIT Later:

    With the HTML you’ve put in your comment, your original code will actually work. The first pass through the .each() will get an undefined value for the .prev() CSS value, but it will appropriately fail your comparison and your original code will actually work. I wouldn’t recommend it as this is a much safer way to do it and probably performs a little better too.

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

Sidebar

Related Questions

I have some divs with divs in them, which I want to loop through
I have this code Math.min(someVariable, 500)) I also have some divs which I have
i have some dynamic code that will make a list of divs. i want
I have this html code and want to use the text in the divs
I have some jQuery code, which attempts to show the first 6 divs on
I have the following code snippet embedded into some of my divs so when
I have some divs: <div class=A>Target</div> <div class=A B>NotMyTarget</div> <div class=A C>NotMyTarget</div> <div class=A
I have some icons inside divs and needed to lay them out with a
I have 2 divs, each has a background image and I want them to
I am using jQuery 1.4.3. I have some divs that have microdata in them

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.