0

I tried to make a simple function which should give me some alerts in the window. I have made an mistake there but cannot find it. Here my code:

var testList = new Array {
  "This",
  "is",
  "a",
  "loop"
};

function printList(list) {
  for (i = 0; i < list.length; i++) {
    alert(list[i]);
  }
  return;
}

$("#button1").click(printList(list));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="button1" type="button" value="Click me!" />

4
  • $("#button1").click(printList(list)); => $("#button1").click(printList(testlist)); Commented Oct 4, 2017 at 9:30
  • new Array { "This", "is", "a", "loop" }; this is an invalid array you will get a error on this line. Change it to "new Array ( "This", "is", "a", "loop" );" Commented Oct 4, 2017 at 9:32
  • Thanks but still don't work :( Commented Oct 4, 2017 at 9:32
  • Just for future reference you can see the problem when you open the console (press F12). The error is listed there. You can also see it in the snippet I edited in to your question. Commented Oct 4, 2017 at 9:39

3 Answers 3

1

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!" />

Sign up to request clarification or add additional context in comments.

4 Comments

Yes but I want to make some alerts. Why do I get alerts when I haven't yet clicked the button?
That's addressed in the third bullet point - you're calling the function immediately
How can I make a reference to it?
You provide a function to the click() method - it's all in the working code example in the answer.
0

You can try this

var testList = ["This", "is", "a", "loop"];

 

$("#button1").click(function(){
printList(testList);
});

    function printList(list) {
      for (i = 0; i < list.length; i++) {
        alert(list[i]);
      }
      return;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="button1" type="button" value="Click me!" />

1 Comment

The point of this website is to educate people. Dumping code with 'Try this' helps no one - not least when it's identical to a more complete answer that was already given.
0
Creating an Array in Javascript
Syntax:
var array_name = [item1, item2, ...];
var testList = ["This", "is", "a", "loop" ];

JavaScript HTML DOM EventListener:
The addEventListener() method attaches an event handler to the specified element.
document.getElementById("button1").addEventListener("click", function(){ printList(testList); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.