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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:48:55+00:00 2026-05-16T02:48:55+00:00

I create li elements dynamically: <ul> <li id=1>1</li> <li id=2>2</li> <li id=3>3</li> […] </ul>

  • 0

I create li elements dynamically:

<ul>
<li id="1">1</li>
<li id="2">2</li>
<li id="3">3</li>
[...]
</ul>

li_id is an array value which returns li id (=1,2,3…)

How can I bind different functions to every li element in code like this:

for (li_id in lids)
{
console.log(li_id);                         
$(li_id).bind('mouseover', function() {
console.log(li_id);
});
}

The above doesn’t work. How to write it properly?

With live() instead of bind() it shows the id of the last element of the lids array, not 1,2,3…[…], like the console.log() outside the $ statement…

  • 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-16T02:48:55+00:00Added an answer on May 16, 2026 at 2:48 am

    http://www.mennovanslooten.nl/blog/post/62

    or

    JavaScript closure inside loops – simple practical example

    Given your HTML, the code can be written in two ways.

    Using jQuery 1.4 Event.data parameter:

    var lids = [1,2,3];
    for (i in lids) {
        var li_id = lids[i];
    
        $('#' + li_id).bind('mouseover', { id: li_id }, function(ev) {
            console.log(ev.data.id);
        });
    }
    

    Or, creating a closure with anonymous function:

    var lids = [1,2,3];
    for (i in lids) {
        var li_id = lids[i];
    
        // an anonymous function 
        (function (id) {
            $('#' + id).bind('mouseover', function() {
                console.log(id);
            });        
        })(li_id); // ...called every time with different id
    
    }
    

    I prefer jQuery Event.data way in this instance.

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

Sidebar

Related Questions

I want to create dynamically an array with N (without knowking N) elements. Something
How can I dynamically create elements, like labels and such, using the google desktop
I am trying to lookup elements within a dynamically created element. For some reason
In PHP you can create form elements with names like: category[1] category[2] or even
Using Titanium Appcelerator I am trying to dynamically create elements and add event listeners
I need to dynamically create my CSS styles for my elements. However, I need
I have a HTML form where i dynamically create elements and set its name
I know when you create an element dynamically, you have to use something like:
In my application, I sometimes have to create elements dynamically, so I create them
I'm creating DOM elements dynamically (more specifically, using jQuery to create a DIV containing

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.