I need to do the following things:
First, fetch data
pathson a server. The data contains an array ofpathand the length is unknown.Then I create a
list-groupwhere eachlist-group-itemstands for apath.Finally, I set the
clickfunction of each of thelist-group-itemwhich displays the correpondingpathin the data array.
Here's the code
var i = 0;
for(path of paths) {
i++;
$("#list_path").append('<a id="list-path-' + i + '" class="list-group-item tooltip-button"">' + "Path " + i + '</a>');
$("#list-path-" + i).click(function() { console.log(path) })
}
However, no matter which list-group-item I click, only the last path element is shown. I guess the problem is that only a pointer of path is assigned to the click function, which in the end of the for loop, always points to the last path.
How can I set the click function to display different path?
Thanks.
$("#list-path-" + i).click(function() { console.log(path) })you capture the variable but not it's content.