0

Possible Duplicate:
In JavaScript, does it make a difference if I call a function with parentheses?

This may sound very basic, but I really don't understand.

I try to take () off from an event handler and it cause a bug in my JavaScript webpage, what is the difference between putting and not putting () at the end when calling a function.

My example is:

document.getElementById("searchField").onkeyup = searchSuggest;

when I changed to searchSuggest(), my function was not working.

Can anyone please explain to me? If I make any part unclear, please ask, thank you.

Community
  • 1
  • 1
Jay
  • 1

1 Answers1

6

When you write:

document.getElementById("searchField").onkeyup = searchSuggest();

You're calling the searchSuggest function and assigning the value it returns to the onkeyup member. In your case, you probably want to assign the function itself to onkeyup, so you should write:

document.getElementById("searchField").onkeyup = searchSuggest;
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479