There's several issues:
- You need to use
() to enclose the array items, not {}.
- The variable is
testList, not just list.
- You're executing the
printList() function on load of the page as you're passing the result of the function, not its reference.
- The
return is redundant when placed at the end of a function
Also note that you should really use console.log for debugging, not alert(). The latter is bad as it coerces data types, so what you see may not necessarily be the actual value. It's also modal, which means it blocks the UI updating. Not to mention it's really annoying when you do it in a loop and you have to click 'Ok' N times.
Finally, you can shorten the array definition by using just [] to declare the elements, like this:
var testList = ['This', 'is', 'a', 'loop'];
function printList(list) {
for (i = 0; i < testList.length; i++) {
console.log(testList[i]);
}
}
$("#button1").click(function() {
printList(testList);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="button1" type="button" value="Click me!" />
$("#button1").click(printList(list));=>$("#button1").click(printList(testlist));