If you want to pass an array, just enter it as a parameter. In Javascript, you can pass numbers, strings, arrays, objects, and even functions as parameters.
See this example for a thumbnail builder implementation: http://jsfiddle.net/turiyag/RxHys/9/
Define arrays first.
var bluearray = [ 'http://fc02.deviantart.net/fs30/f/2008/056/8/0/Purple_hair___Bipasha_Basu_by_mstrueblue.jpg', 'http://static.becomegorgeous.com/img/arts/2010/Feb/20/1805/purple_hair_color.jpg', 'http://img204.imageshack.us/img204/6916/celenapurpleqp7.jpg' ]; var greenarray = [ 'http://25.media.tumblr.com/tumblr_m7fqmkNEhc1qlfspwo1_400.jpg', 'http://www.haircolorsideas.com/wp-content/uploads/2010/12/green-red-hair.jpg', 'http://fc02.deviantart.net/fs71/i/2010/011/9/c/Neon_Yellow_and_Green_Hair_by_CandyAcidHair.jpg' ];
Then, when the DOM is loaded, call the functions to load the thumbnails.
$(function() { addThumbs(bluearray); addThumbs2(greenarray); });
addThumbs uses jQuery for each function to make things cleaner. I believe this looks better and it is better to use a regular Javascript for loop.
function addThumbs(paths) { $.each(paths,function(index, value) { $("div").append('<img src="' + value + '" />'); }); }
But if you are a fan of your own Javascript, the normal loop loop is implemented in addThumbs2
function addThumbs2(paths) { for(index in paths) { $("div").append('<img src="' + paths[index] + '" />'); } }
source share