Introduction to Events
When you write JavaScript in the browser, you’re writing event-driven code. Most of your code will be executed when something happens, such as having content slide in when a user clicks a link.
The term “fires” is often used with events. Example: “The keypress event fires the moment you press a key”.
Here are some common DOM events:
Mouse Events | Keyboard Events | Form Events | Document/Window Events |
---|---|---|---|
click | keypress | submit | load |
dblclick | keydown | change | resize |
mouseenter | keyup | focus | scroll |
mouseleave | blur | unload |
jQuery Syntax For Event Methods
In jQuery, most DOM events have an equivalent jQuery method.
To assign a click event to all paragraphs on a page, you can do this:
$("p").click();
The next step is to define what should happen when the event fires. You must pass a function to the event:
$("p").click(function(){ // action goes here!! });