In jQuery, is it possible to have a variable as a constant? I know it is not possible in many other languages, but jQuery never ceases to surprise me. Maybe this isn't the right question, anyway, I am trying to use a loop index as an ID, of which the ID calls a method. Through each loop the ID changes and I'm trying to trigger the same function from a set of different a elements. How can I change my code to have a dynamic caller?
for(var i in data.items) {
var id = $(i);
var viewMore = $("<a href=# id=" + id + ">View More</a>");
id.on('click', function(x) {
// do something
}
}
for (var i in data.items) { var id = $(i); var viewMore = $("<a href=# id=" + o + ">View More</a>"); viewMore.on('click', function (x) { // do something }) }