I have a function for a jQuery plugin that loop through some images.
//load images
function ImageSettings(config, fileNames) {
//loop through the array in the second argument
for (var x = 0; x < fileNames.length; x++) {
//create image
$("<img />").attr({
id: fileNames[x],
src: config.imgDir + fileNames[x] + config.imgFormat,
title: fileNames[x] + " layout"
}).appendTo("#" + config.imgContainer);
}
};
Further down my plugin, I need to pass the image attr ID inside a unordered list item , but my variable is located inside my function called fileNames.
so if use:
$(config.sidebar).append('<ul>' + fileNames + '</ul>');
//I get a string like: home, about, contact but I need it to be styled in a list item
If I use the split method to remove the " , " then I get an unknown method split. So, is it possible to pass the function and variable inside? like for instance so I work around the unknown method?
$(config.sidebar).append('<ul>' +
ImageSettings(fileNames[x]).each(function() {
$( this ).wrap( "<li>" );
+ '</ul>');
I thought about using something like $('img').attr('id') and then style this in a list item but because I will have several images on my page but not all will be loaded and not all will need to be wrapped in a list item. Hence the reason why I would like to use my variable inside the function. Thank you.
.eachto it. And.eachdoesn't return a string, so you can't concatenate it.fileNamesoutside the function it's declared in.