How to access an object property from an array of objects in a click handler?

Sai

I have created a number of instances of a object constructor which I have placed into an array and looped over to display into a list. Now I want to select a name property from that list to use in an onclick event handler (not shown in this code). I would like to know how to access the name property in the click handler. This is what i have tried so far but i keep getting undefined.

console.log(contactarray[i].name);
console.log(contactarray.name);

code

$(document).ready(function() {

function ContactList (name, email, number,address) {
    this.name = name;
    this.email = email;
    this.number = number;
    this.address = '6539 Wilton Ave Culver City CA 90234';
}

var christian = new ContactList('Christian', '[email protected]', '323-555-124');
var rich = new ContactList('Rich', '[email protected]', '323-555-124');
var scott = new ContactList('Scott', '[email protected]', '323-555-124');
var danny = new ContactList('Danny', '[email protected]', '323-555-124');
var taka = new ContactList('Taka', '[email protected]', '323-555-124');
var tim = new ContactList('Tim', '[email protected]', '323-555-124');
var patrick = new ContactList('Patrick', '[email protected]', '323-555-124');
var jacques = new ContactList('Jacques', '[email protected]', '323-555-124');

var contactarray = [christian, rich, scott, danny, taka, tim, patrick, jacques];

for (i = 0; i < contactarray.length; i++) {
    $('#contacts').append('<li class="itemname" id="'+i+'"><a href="#">' + contactarray[i].name + '</a></li>');
}

My issue is getting access to the name property of one of the list items when it is clicked.

Michael Geary

What you've run into is the classic problem with asynchronous JavaScript events in a loop. This was not apparent from your question because it didn't have a click handler anywhere, but it became obvious from your subsequent comment. Always provide enough information in a question to reproduce the actual problem. Simplified code is good, but not when the essential problem area is simplified out!

The easiest solution is to call a function for each loop iteration. Each time you call a function, it creates a "closure" which captures all the parameters and local variables in that function, even for asynchronous code like a click handler which gets called later.

Since you're using jQuery, you can do that with $.each(), or you could create and call a function of your own in a for loop, as long as you call it for every loop iteration.

Here's a working solution. I also simplified your code a bit by putting the contact items directly inside the array instead of creating a named variable for each one. And I changed the name of your ContactList constructor to ContactItem because it represents an individual item and not a list:

function ContactItem( name, email, number,address ) {
    this.name = name;
    this.email = email;
    this.number = number;
    this.address = '6539 Wilton Ave Culver City CA 90234';
}

var contactarray = [
    new ContactItem('Christian', '[email protected]', '323-555-124'),
    new ContactItem('Rich', '[email protected]', '323-555-124'),
    new ContactItem('Scott', '[email protected]', '323-555-124'),
    new ContactItem('Danny', '[email protected]', '323-555-124'),
    new ContactItem('Taka', '[email protected]', '323-555-124'),
    new ContactItem('Tim', '[email protected]', '323-555-124'),
    new ContactItem('Patrick', '[email protected]', '323-555-124'),
    new ContactItem('Jacques', '[email protected]', '323-555-124')
];

var $contacts = $('#contacts');

$.each( contactarray, function( i, contact ) {
    var $contact = $(
        '<li class="itemname" id="' + i + '">' +
            '<a href="#">' + contact.name + '</a>' +
        '</li>'
    );
    $contact.click( function() {
        alert( contact.name );
    }).appendTo( $contacts );
});

Updated fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access property of array of objects

From Dev

How to remove a specific Object from an array of Objects, by object's property?

From Dev

how to access objects in an array by a property of those objects?

From Dev

How to sort array of objects according to a property of the object

From Dev

Angular expression get property of object from equal property in array of objects

From Dev

Access object property in array

From Dev

Convert array of objects to array of specific property from each object

From Dev

How to access property of an object that was retrieved from database?

From Dev

How to access an object from a list of objects

From Dev

how to access to "$(this)" object from event handler passed to "on" method?

From Dev

How to access a value from an array of objects in localStorange

From Dev

Create dictionary from an array of objects using property of object as key for the dictionary?

From Dev

How to access a property of an object (stdClass Object) member/element of an array?

From Dev

How to access object property inside an nested array of object?

From Java

How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

From Dev

How to access an object literal property from another property?

From Dev

How to get property from an array of object ?

From Dev

How to get property from an array of object ?

From Dev

How to get object with largest property from array?

From Dev

how is Array object different from other objects

From Dev

how is Array object different from other objects

From Dev

Can't access property if object is taken from array

From Java

How to group array of objects by value of object and sum the result in new property

From Dev

Get a Object property from array of objects when I have the value of another property in the same object

From Dev

How to delete a property from prototype in an array of objects from MongoDB?

From Dev

AngularJS - How to access an array property inside an object with ng-repeat

From Dev

How to remove duplicates from a list of custom objects, by a property of the object

From Dev

How can I access an array of objects within a JSON object?

From Dev

How to access object's properties when looping through an array of objects?

Related Related

  1. 1

    How to access property of array of objects

  2. 2

    How to remove a specific Object from an array of Objects, by object's property?

  3. 3

    how to access objects in an array by a property of those objects?

  4. 4

    How to sort array of objects according to a property of the object

  5. 5

    Angular expression get property of object from equal property in array of objects

  6. 6

    Access object property in array

  7. 7

    Convert array of objects to array of specific property from each object

  8. 8

    How to access property of an object that was retrieved from database?

  9. 9

    How to access an object from a list of objects

  10. 10

    how to access to "$(this)" object from event handler passed to "on" method?

  11. 11

    How to access a value from an array of objects in localStorange

  12. 12

    Create dictionary from an array of objects using property of object as key for the dictionary?

  13. 13

    How to access a property of an object (stdClass Object) member/element of an array?

  14. 14

    How to access object property inside an nested array of object?

  15. 15

    How to find multiple indexes in array of objects by the value of an object property that nests in a property of array of objects?

  16. 16

    How to access an object literal property from another property?

  17. 17

    How to get property from an array of object ?

  18. 18

    How to get property from an array of object ?

  19. 19

    How to get object with largest property from array?

  20. 20

    how is Array object different from other objects

  21. 21

    how is Array object different from other objects

  22. 22

    Can't access property if object is taken from array

  23. 23

    How to group array of objects by value of object and sum the result in new property

  24. 24

    Get a Object property from array of objects when I have the value of another property in the same object

  25. 25

    How to delete a property from prototype in an array of objects from MongoDB?

  26. 26

    AngularJS - How to access an array property inside an object with ng-repeat

  27. 27

    How to remove duplicates from a list of custom objects, by a property of the object

  28. 28

    How can I access an array of objects within a JSON object?

  29. 29

    How to access object's properties when looping through an array of objects?

HotTag

Archive