Help me understand function(event) with an onEvent

Can someone explain to me why on the onEvent block it shows says function(event) after the id and type? It really threw my students last year, especially when they would change it to text mode. I wanted to explain it better this year. Thanks!

Yeah, I think that these are beyond the scope of a CSP course and were a bit too complicated to explain to students at this level so basically avoids any real explanation and just gives a bunch of examples of how to use them and just sticks with that.

I’ll take a shot at explaining it though…

Here’s an example of an onEvent block:
onEvent(“forward”, “click”, function(event) {

onEvent itself is not a function definition but a function call.
It is really registering an event handler. Meaning, it is telling the program what code to execute when a given even occurs.
The “function” keyword here is actually a argument (parameter) to the onEvent function. It may seem strange but we are actually passing an entire function as a parameter to another function! Now you see why didn’t want to go down the road of trying to explain all of this. :slight_smile:

Anyway, the idea is that we call onEvent and pass in a function that gets called whenever that event actually occurs on the item with the ID that we want.

Hope that helps!

  • Mitch

Yes, I understand. That at least helps me frame it better in my mind. Thanks!