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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:32+00:00 2026-06-15T12:53:32+00:00

I have the following markup containing 10 pre elements with the class indent :

  • 0

I have the following markup containing 10 pre elements with the class indent:

​<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>
<pre class="indent"></pre>​

I’m using the following jQuery .each() function to iterate through each element:

​$(function(){    
    $.each(".indent", function(index){
       alert(index); 
    });    
});​

I would expect to see 10 alerts, however I only see 7

— See Fiddle —


However, this works as expected with $(".indent").each():

$(function(){    
    $(".indent").each(function(index){
       alert(index); 
    });    
});​

— See Fiddle —


Looking at the $.each() documentation, I understand theres a difference:

The $.each() function is not the same as $(selector).each(), which is
used to iterate, exclusively, over a jQuery object.

But I don’t understand why in this instance, it doesn’t iterate through all elements.

Why is this happening?

  • 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-15T12:53:33+00:00Added an answer on June 15, 2026 at 12:53 pm
    $.each(".indent", function(index){
    

    doesn’t iterate over the elements of $('.indent') but over the ".indent" string whose length is 7 chars.

    See reference


    A more detailed explanation based on jQuery source code :

    jQuery first checks if the first parameter, obj (here your string), has a length :

    var ...
            length = obj.length,
            isObj = length === undefined || jQuery.isFunction( obj );
    

    Your string having a length (and not being a function), isObj is false.

    In this case, the following code is executed :

    for ( ; i < length; ) {
        if ( callback.call( obj[ i ], i, obj[ i++ ] ) === false ) {
            break;
        }
    }
    

    So, given the function f, the following code

    $.each(".indent", f);
    

    is equivalent to

    for (var i=0; i<".indent".length; i++) {
        var letter = ".indent"[i];
        f.call(letter, i, letter);
    }
    

    (you can log the letters using var f = function(i,v){console.log(v)}; or be reminded one of the subtleties of call using var f = function(){console.log(this)};)

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

Sidebar

Related Questions

I have the following markup: <div class=class1><p>line1</p><p>line 2</p></div> With jQuery, how can I take
If I have some xml containing things like the following mediawiki markup: ...collected in
I have the following markup: <div id=routes> <ul id=routesList> <li class=routes>Route 1</li> <li class=routes>Route
I have the following markup: <p class=managebox> <button value=Add page> <img src=page_add.png alt=Add more
I have following markup which is generated dynamically using C# in my asp.net MVC2
I have the following markup: <li id=fragment-3 class=slides virtualsation ui-tabs-panel style=> <h1>Virtualisation</h1> <p>By implementing
I have the following markup (HTML with native SVG): <!doctype html> <!-- ... html-Elements
I have the following markup. I would like to add class_A to <p class=subitem-text>
I have the following markup: http://www.webdevout.net/test?011 As you can see, the li element is
The following markup will have some extra elements injected into it (i.e.: a div

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.