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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:00:29+00:00 2026-05-23T05:00:29+00:00

I have a list and am using jQuery to get every LI in the

  • 0

I have a list and am using jQuery to get every LI in the list:

$('ul li')

How do i get it so that I run code after each element is found, but not an event; like this:

$('ul li').code(function() {
    alert('this will alert for every li found.');
});

Whats the best way for me to do 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-23T05:00:30+00:00Added an answer on May 23, 2026 at 5:00 am

    There are two types of loops within jQuery: explicit and implicit.

    Explicit Loops

    Explicit loops are loops that you explicitly invoke. If you have an array of values that you want to cycle through, you could use the $.each() method:

    $.each( [ '#a', '#b', '#c' ], function( index, value ) {
      $( value ).css("color", "red");
    });
    

    jQuery will then cycle through each of the values in your array, and run an anonymous function afterwards. It will pass two values into this function: the current index of the value it’s touching, and the value itself. So if we’re on our first run through, index will be 0, and value will be ‘#a’.

    So if we started with:

    <p id="a">A</p>
    <p id="b">B</p>
    <p id="c">C</p>
    

    After running our explicit loop, our result would be:

    <p id="a" style="color:red">A</p>
    <p id="b" style="color:red">B</p>
    <p id="c" style="color:red">C</p>
    

    Of course you’re not limited to arrays and other hand-crafted collections when using the $.empty() method. You can also chain this method right off your selector (note, technically this is a different $.each() method):

    $("ul li").each(function( index ){
      $(this).css("color", "red");
    });
    

    This method is specifically for cycling over the jQuery object. Each iteration exposes the current element as this within your anonymous function. So revisiting our earlier example, we could have done the following:

    $("#a, #b, #c").each(function( index ){
      $(this).css("color", "red");
    });
    

    So for each element matched, we will have our anonymous function executed. jQuery will pass us an index so that we can track our progress through the matched elements. On the first run through, index will be 0, and this will refer to #a. On the second time through, index will be 1, and this will refer to #b.

    While explicit loops are enticing, note that they may add unnecessary overhead to your application. jQuery already performs a loop on its own when it has several matched elements. This loop is called an implicit loop, and at times we can use it to accomplish our needs, while boosting performance.

    Implicit Loops

    Implicit loops are done automatically within jQuery itself, for instance with your code:

    $("ul li").css("color", "red");
    

    jQuery performed a loop over all of the matched li elements and set their color to "red". We didn’t have to specifically ask for a loop – it just does one implicitly. But suppose "red" was far too simple, and we wanted to interact with each item individually – we can do that by passing in a function as the second argument:

    $("ul li").css("color", function( index, value ){
      alert( "We're inside LI number " + index );
      return this.innerHTML;
    });
    

    Our function will be passed two arguments by jQuery: an index for the current element, and the current rgb value of the color on that element. You could use either one of these for the internal logic – I’m simply returning the innerHTML of the li. What we return will become the new value for color on this element.

    So if our unordered list looked like this:

    <ul>
      <li>Red</li>
      <li>Green</li>
      <li>Blue</li>
    </ul>
    

    After jQuery’s implicit loop finishes, the first li will be red, the second will be green, and the last will be blue.

    <ul>
      <li style="color:Red">Red</li>
      <li style="color:Green">Green</li>
      <li style="color:Blue">Blue</li>
    </ul>
    

    You can see this online at http://jsbin.com/egihun/2/edit

    Depending on what method you’re calling, different arguments will be passed to your function. Be sure to study the documentation to know what information is available to you during these implicit loops.

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

Sidebar

Related Questions

I have written a cascading drop down list using JQuery. The code that I
I have created a dynamic list picker script using Jquery 1.3 and PHP that
I have a hierarchical UL list. How to - using jQuery - get the
I have a sortable list using JQueryUI and use the JQuery Each function to
I have a sortable list of folders using JQuery UI. The thing is that
We have a sortable list using JQuery UI Sortable that we are trying to
I'm using jQuery to get a click on an 'a' element. I have a
i'm using jquery. i have a anchor list. i'm enumerate anchors, if it visited,
I have an unordered list, and am using jquery to find the last ul
I have a list of items that are displayed using a ListView from a

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.