Outline
My simple application takes text from a text box and calls an asp.net page method using AJAX. A list of users that partially match that text and their userID is returned. I wish to fill an existing DIV on the page with a DIV for each result, containing the users name. Each DIV then needs a click event to be bound to it that can call a javasacript method that can then have access to the key - the userID.
Code
function SearchButtonHandler(event) {
PageMethods.SearchInstructors($("#txtInstructorName").val(), SearchCallback);
return false;
}
The text to search is passed to the ajax call and the SearchCallback method receives a json array of objects containing an integer 'UserID' and a string 'Fullname'.
This is the first way I did it, creating a 'fake' attribute on the DIV and having the click handler extract it:
function RowClicked() {
alert($(this).attr("userid"));
}
function SearchCallback(data) {
var collection = data.Result;
if (collection.length == 0) { //tell the user there were none
$("#resultlist").hide();
$("#nonefound").show();
} else {
$("#nonefound").hide();
$("#resultlist").empty();
//method 1 below
$.each(collection, function(index, item) {
$("#resultlist").append($("<div></div>").html(item.FullName).attr("userid", item.UserID).click(RowClicked));
});
$("#resultlist").show();
}
}
I don't really like creating 'fake' attributes but came up with the following method:
$("#resultlist").append($("<div></div>").html(item.FullName).click(function() {
alert(item.UserID);
}));
So I have two alternatives that both work fine.
Question 1:
Is this second one less efficient? It seems to me that it needs to create a function for each DIV tag. Or is there another way to do it?
Question 2 (leading on from 1):
How is 'item' in scope when the click event is triggered? It clearly works, I just can't see how that can be in scope!