I want to execute a function that appends (jQuery) a li to an ul. Now I want to pass a string as a parameter that looks like:
v.name + " by " + v.artist
The problem is that the v.name and v.artist don't exist outside the function, because the function contains an .each loop:
function showAllItems (data, displayText) {
$("ul.result-list").empty();
$.each(data.results, function (k, v) {
var name = eval(displayText);
if ($("ul.chosen-list input#" + v.name).length == 0) {
$("ul.result-list")
.append($("<li>")
.append($("<input>", {id: v.name, value: v.name, type: "checkbox"}))
.append($("<label>", {html: name, for: v.name, class: "clickable"})));
}
});
}
What I am using now is eval(displayText) to convert a string into a real variable. But now I can't pass something like I said earlier (v.name + " by " + v.artist).
In short, I want to print a string containing variables that don't exist until the .each loop.
I hope someone can help me!
Thanks in advance,
Ian Wensink