You could use Event Delegation and attach the onclick listener to the ul
and then inside of the listener function use event.element().id to grab
the id of the item that was clicked on. This way, you can build the
content using innerHTML (or .update() which is basically the same thing)
and only have one event listener to worry about (reduces memory footprint).
I'm on a roll tonight, so I'll do some Googling, and see what I can do. Thanks!
I understand closures. However, I haven't found an elegant way to
solve the problem. This won't work:
function populateContactsList( contacts )
var theUL = document.getElementById("addr-book-index");
var thisLI = null;
for ( var i = 0; i < contacts.length; i++ ) {
thisLI = document.createElement("li");
thisLI.appendChild( document.createTextNode( contacts[i].name ) );
theUL.appendChild( thisLI );
Event.observe( thisLI, "click", function() {
openContact( contacts[i].id );
} );
}
}
...because every call to openContact() will receive 3 as the parameter
- the value of contacts[i].id at the time the outer function exits.