I have instantiated multiple object constructors and stored them in an array which I loop over to display as a list. Now, I am trying to extract the name property from that list to use in an onclick
event handler (not included in this code). I am unsure of how to access the name property within the click handler. Here is what I have attempted so far, but it returns 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', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99faf1ebf0eaedf0f8f7d9fce1f8f4e9f5fcb7faf6f4">[email protected]</a>', '323-555-124');
var rich = new ContactList('Rich', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4968d878ca4819c8589948881ca878b89">[email protected]</a>', '323-555-124');
var scott = new ContactList('Scott', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7a4b4b8a3a397b2afb6baa7bbb2f9b4b8ba">[email protected]</a>', '323-555-124');
var danny = new ContactList('Danny', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a4a1aeaeb980a5b8a1adb0aca5eea3afad">[email protected]</a>', '323-555-124');
var taka = new ContactList('Taka', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2a3f353f1e3b263f332e323b703d3133">[email protected]</a>', '323-555-124');
var tim = new ContactList('Tim', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f6ebefc2e7fae3eff2eee7ace1edef">[email protected]</a>', '323-555-124');
var patrick = new ContactList('Patrick', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32425346405b515972574a535f425e571c515d5f">[email protected]</a>', '323-555-124');
var jacques = new ContactList('Jacques', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6bcb7b5a7a3b3a596b3aeb7bba6bab3f8b5b9bb">[email protected]</a>', '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 challenge lies in accessing the name property of a specific list item when it is clicked.