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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:22:04+00:00 2026-05-29T12:22:04+00:00

I got this for(m in _data.data) { var _person= new Person(_data.data[m].Properties); //return a person

  • 0

I got this

for(m in _data.data)
    {
        var _person= new Person(_data.data[m].Properties); //return a person object
        $('ul.v_list').append("<li>"+_person.person_name+'</li>').css("cursor", "pointer");

        $('ul.v_list').find('li:last').bind('click', function(){onPersonChanged(_person)});
    }

And this another function

 function onPersonChanged(person){
        alert("You have clicked " + person.person_name);
   };

Everytime is passing the last object created in the Person Object to the onPersonChanged function so it alerts the last name in the list if I click in the first li or another li element nested in the ul. eg.:

<ul class='v_list'>
     <li>James</li>
     <li>Paul</li>
     <li>Bonnie</l>
</ul>

When I do a click in Paul I expect an alert showing “You have clicked Paul” instead I always get the last one li in the list: “You have clicked Bonnie”. How can I get the correct alert for the selected li?

  • 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-29T12:22:05+00:00Added an answer on May 29, 2026 at 12:22 pm

    Your onPersonChanged function is being called after the loop has been executed therefore only the last _person in the loop will be passed through. Look up “closures” in javascript.

    To fix it you should do the following:

    for(m in _data.data)
    {
        var _person= new Person(_data.data[m].Properties);
        (function(p) { 
            $('ul.v_list').append("<li>"+p.person_name+'</li>').css("cursor", "pointer");
    
            $('ul.v_list').find('li:last').bind('click', function(){onPersonChanged(p)});
    
        })(_person);
    }
    

    this will evaluate the current value of _person as it is at that point in the loop as opposed to evaluating it after the loop has finished.

    The (function(p) { ... })(_person); is an anonymous self-executing function. this creates a new sub-scope in your for loop which will preserve the value of _person correctly.

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

Sidebar

Related Questions

I'm having problems getting the new data from the RandomNumbers List I got this
I want to append data to selectbox. But, I got this error... Error: $().attr(value,
I got this formula from a data structure book in the bubble sort algorithm.
I got this parameter: $objDbCmd.Parameters.Add(@telephone, [System.Data.SqlDbType]::VarChar, 18) | Out-Null; $objDbCmd.Parameters[@telephone].Value = $objUser.Telephone; Where the
I've got this simple Perl script: #! /usr/bin/perl -w use strict; use Data::Dumper; my
Ok, I've got a list like this (just a sample of data): data =
I've got 2 select statements, returning data like this: Select 1 col_a col_b Select
I tried to use this on my class library mylib.core.data.dll and got a successful
I got a view that inherits : System.Web.Mvc.ViewPage<IEnumerable<MyProjects.Models.MyAccountWrapper>> In this view I list data
Got this line of code here but its not working. private void Button_Click(object sender,

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.