I have a function that binds click events to all anchor tags whose ids are contained in an array called guids.
I iterate through all guids and am trying to make displayDetails() function bind to the click event. I have tried .click(function()) and .bind('click', function()) but neither one is working correctly. Function displayDetails() keeps getting executed within the bind.
The problem is that when I run the page the displayDetails() gets called for each guid. I don't want to call displayDetails() each time. I only want to bind it, and not execute the displayDetails() in the loop.
How do I make displayDetails() get bound to the click event and not get called in the loop when binding is occurring?
function bindLinks() {
for (var idx = 0; idx < guids.length; idx++)
{
$("#" + guids[idx]).click(function (e) {
displayDetails(guids[idx], orders[idx]);
});
}
}
EDIT: I broke up my function into two separate functions and now I am getting the correct popup displayed from displayDetails() but now the function is called many times based on the fixed index. I am getting multiple alerts where instead I want the function called only once.
function bindLink(idx) {
$("#" + guids[idx]).click(function () {
displayDetails(guids[idx], orders[idx]);
});
}
function bindLinks() {
for (var idx = 0; idx < guids.length; idx++)
{
bindLink(idx);
}
}