Reputation: 4105
I am trying to create a closure in my for loop, how ever, it keeps returning a function instead of a string.
$(function() {
var addID = function(id) {
var temp = id;
return function() {
return "http://localhost:3000/board/raptrex/" + temp;
}
};
$.get("http://localhost:3000/board/raptrex", function( data ) {
console.log(data);
var $deleteUL = $('#delete');
for (var i = 0; i < data.length; i++) {
var url = addID(data[i]._id);
console.log(url);
var $item = $('<li></li>').text(data[i].name).click(function() {
$.ajax({
type: "DELETE",
url: url
})
.done(function( msg ) {
alert( "Deleted: " + msg );
});
});
$deleteUL.append($item);
}
});
});
This returns http://localhost:3000/function%20()%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%22http://localhost:3000/board/raptrex/%22%20+%20temp;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
when I click on my li element
Upvotes: 0
Views: 35
Reputation: 46
You're returning a function instead of a string with addID
. Try the following:
var addID = function(id) {
var temp = id;
return "http://localhost:3000/board/raptrex/" + temp;
};
Upvotes: 2