So far, you've researched browser events
and you've taken a peak
at them being logged
Now it's time to actually listen for
and respond to them with jQuery
There are 3 items you need in order to
listen for events and react to them
You need: the target element to listen to
the event we want to react to
and the actions to take in response
Let me show you an example
I need the target element that jQuery
will be listening to for events
I'll use jQuery to select the input field
Next, I call the "on method"
This on method is where the magic happens
It's the primary way the jQuery uses
to set up event listeners
The first argument to the on method
is the event I want to listen for
In this example, it's keypress
but it could also be click, change
and mouseover to name a few.
And finally,
I need to pass a function with
the actions I want to happen in response
This function is called a "callback"
The callback function being passed
to the on method
is just a regular JavaScript function
and therefore, can contain
any Javascript code you want
from altering page content
to analytics code
I'll change the contents of the function
to alter the colour of the page
And I'll test it
Bam
Looks good!
In the next quiz, you're going to create
your own event listener