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

  • Home
  • SEARCH
  • 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 9014433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:30:21+00:00 2026-06-16T03:30:21+00:00

I am having some trouble understanding how selectors and indexes work in JQuery. I

  • 0

I am having some trouble understanding how selectors and indexes work in JQuery. I was hoping to write some functions that manipulate html elements based on class attributes, and I realized that when I try to use the index() method or prevAll().length to get the position of an element within a set of elements, I’m not always getting the expected result.

Below, I have a piece of jquery code that does something for everything with the class .element_list, and then within that something for everything with the class .element_list_item. I have two .element_lists, A and B, and all this does is alert me the indexes of the .element_list_items. For list A, the indexes come back as 0 and 1, as expected. In list B, I added a thead element with no class attribute, so in my mind it shouldn’t be grabbed by my jquery code, but the two .element_list_items come back with indexes of 1 and 2, instead of 0 and 1 as expected.

Why is that? My selector is looking for elements wtih the class .element_list_item and a class equal to the contain .element_list’s ID, so why would the addition of the thead affect the positions of the tbodies?

Any help understanding this is appreciated.

<script language="javascript" type="text/javascript">
$(document).ready(function ()
{
$('.element_list').each(function()
{
    var list_id = $(this).attr('id');
    var first_item_id = $(this).find('.' + list_id + '.element_list_item:eq(0)').attr('id');

    alert('List and first list item:  ' + list_id + ' - ' + first_item_id);        

    $(this).find('.' + list_id + '.element_list_item').each(function()
    {
        var list_item_id = $(this).attr('id');
        var index = $(this).prevAll().length;

        alert('List item and its index:  ' + list_item_id + ' - ' + index);
    });
});
});
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="" enableviewstate="true">
<br />
<br />
<table class="table_noborder">
    <thead>
        <tr>
            <td>List A</td>
        </tr>
    </thead>
    <tbody id="list_A" class="element_list">
        <tr>
            <td>
                <table>
                    <tbody id="list_A_item0" class="element_list_item list_A">
                        <tr>
                            <td>First Item</td>
                        </tr>
                    </tbody>
                    <tbody id="list_A_item1" class="element_list_item list_A">
                        <tr>
                            <td>Second Item</td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

<table>
    <thead>
        <tr>
            <td>List B</td>
        </tr>
    </thead>
    <tbody id="list_B" class="element_list">
        <tr>
            <td>
                <table>
                    <thead>
                        <tr>
                            <td>Header</td>
                        </tr>
                    </thead>
                    <tbody id="list_B_item0" class="element_list_item list_B">
                        <tr>
                            <td>First Item</td>
                        </tr>
                    </tbody>
                    <tbody id="list_B_item1" class="element_list_item list_B">
                        <tr>
                            <td>Second Item</td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>
</form>
</body>
</html>
  • 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-16T03:30:22+00:00Added an answer on June 16, 2026 at 3:30 am

    using the .index() with no arguments will get it’s index compared to all siblings. You can pass in a selector to for it to check in comparison with the collection

    so

    $('#list_B_item0').index() // will return 1 since thead(it's prev sibling) is in index 0
    

    but

    $('#list_B_item0').index('.element_list_item.list_B') //  returns 0 since it will compare it's index with the collection
    

    It can also be done the other way

    $('.element_list_item.list_B').index('#list_B_item0') // returns 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble understanding the delegate/data source methodology. I understand that they exist
Hey I'm having some trouble understanding how Recursive Algebraic Types work and how to
I'm very new to JQuery, and I'm having some trouble understanding how to do
I'm having some trouble understanding why this code won't work. I got it from
I'm having some trouble understanding how ko.mapping.fromJS and ko.mapping.toJS work. Here the explanation of
Just learning PHP and I'm having some trouble understanding mysql_query. My understanding is that
I'm having some trouble understanding the differences between how Expressions and Funcs work. This
I'm having some trouble understanding this piece of code that my professor used as
I'm trying out some MacOS programming and having some trouble understanding how bindings work
I'm having some trouble understanding the rules for array broadcasting in Numpy. Obviously, if

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.