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', 'christian@example.com', '323-555-124');
var rich = new ContactList('Rich', 'rich@example.com', '323-555-124');
var scott = new ContactList('Scott', 'scott@example.com', '323-555-124');
var danny = new ContactList('Danny', 'danny@example.com', '323-555-124');
var taka = new ContactList('Taka', 'taka@example.com', '323-555-124');
var tim = new ContactList('Tim', 'tim@example.com', '323-555-124');
var patrick = new ContactList('Patrick', 'patrick@example.com', '323-555-124');
var jacques = new ContactList('Jacques', 'jacques@example.com', '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.