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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:05:07+00:00 2026-05-31T05:05:07+00:00

I have created an array object with some dummy properties, I have have also

  • 0

I have created an array object with some dummy properties, I have have also dynamically created some list items that I would like to attach a click handler to. When a list item is clicked I would like to present the appropriate data inside #container using the template Ive set up. Im assuming I can use the index from the for loop of dynamic list items and some how use this to show the correct object properties? If you could advise me where I have gone wrong with this that would be great, sorry but Ive lost my way a little with this.

$(document).ready(function () {

    var data = [

    {
        name: 'kyle',
        age: 23,
        sex: 'male'
    }, {
        name: 'james',
        age: 19,
        sex: 'male'
    }, {
        name: 'catrina',
        age: 28,
        sex: 'female'
    }];

    var template = $('#template').html();

    // Links created dynamically
    for (var i = 0; i < 3; i++) {
        var link = '<li>Link ' + i + '</li>';
        $('#links').append(link);
    }

    // When li is clicked show related data properties, li[0] = data[0], li[1] = data[1] ...
    $('li', '#links').live('click', function (e) {
        $.each(data, function (index, value) {
            $('#container').append(data.name[i], data.age[i], data.sex[i]);
        });

        $('#container').html(data);
    });

});

Code can be found here http://jsbin.com/otirax/6/edit

  • 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-31T05:05:09+00:00Added an answer on May 31, 2026 at 5:05 am

    The easiest thing is to add a data item to each link when you create it that contains the index. You can then fetch that index later upon click:

    for(var i = 0; i < 3; i++){
        var link = '<li data-index="' + i + '">Link ' + i + '</li>';
        $('#links').append(link);
    }
    

    Then, in your click handler, you can retrieve that value:

    $('li', '#links').live('click', function(e){ 
        // retrieve index
        var index = $(this).data("index");
        // retrieve piece of data for this link
        var thisData = data[index];
        // do whatever you need to with the data for this link here
        // thisData.name
        // thisData.age
        // thisData.sex
    });
    

    This way of storing and then retrieving the index provides a permanent index value set on each li at the time you create it. If you will never reorder the li tags and they are the only objects at that level, you can simplify it a bit by using the .index() jQuery method that will calculate the index for a given li tag dynamically when needed by just counting how many siblings come before it:

    // Links created dynamically
    for (var i = 0; i < 3; i++) {
        var link = '<li>Link ' + i + '</li>';
        $('#links').append(link);
    }
    
    $('li', '#links').live('click', function(e){ 
        // retrieve index
        var index = $(this).index();
        // retrieve piece of data for this link
        var thisData = data[index];
        // do whatever you need to with the data for this link here
    });
    

    FYI, .live() has been deprecated and one should use .on() for jQuery 1.7+ or .delegate() for earlier versions of jQuery.

    Your .live() code would be this with .on():

    $('#links').on('click', 'li', function(e){ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an array in php that prints this Array ( [mark] =>
Hi I have created a setDecorator() which is something like this: $timeSu->setDecorators(array('ViewHelper', 'Description', 'Errors',
I am currently trying to pass an array that I have created in Javascript
I'm a beginner i have some problem with NSArray. I created an array and
In short, I have an object called PersonList that has a List of Person
I have created a task in Symfony 1.4 that loads in some CSV files
i have created Array of Linkbutton and when user click on link button it
I have created an array Man: public main blah blah{ man = man[10]; }
I have an array created with this code: var widthRange = new Array(); widthRange[46]
How do I pass an array I have created on the server side onto

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.