jQuery Event Methods
jQuery is specifically designed to react to actions happening on a webpage.
What are Events?
Events on a webpage are actions by visitors that the page can respond to.
An event is like a specific point in time when something occurs.
Examples:
- moving a mouse over an element
- selecting a radio button
- clicking on an element
The word "fires" or "fired" is commonly used when talking about events. For instance, when you press a key, it triggers the keypress event immediately.
Here are a few typical events that occur in the Document Object Model (DOM):
| 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, there's typically a matching jQuery function for most DOM events.
To make something happen when you click on all the paragraphs on a webpage, you can do this:
The following step is to specify what needs to occur when the event takes place. To do this, you must provide a function for the event.
$("p").click(function(){
});
Commonly Used jQuery Event Methods
$(document).ready()
The $(document).ready() method lets us run a function once the entire document is ready. You can find more information about this event in the jQuery Syntax chapter.
click()
The click() method links a function that handles events to an HTML element.
The action happens when someone clicks on an HTML element.
In this example, it means that when someone clicks on a paragraph (<p> ) on a webpage, the paragraph that was clicked on should disappear.
dblclick()
The dblclick() method links a function that handles events to an HTML element.
This function runs when the user double-clicks on an HTML element.
mouseenter()
The mouseenter() method connects a special function to an HTML part when you move your mouse over it.
The function runs when the mouse arrow goes into the HTML element.
mouseleave()
The mouseleave() method connects a special function to an element in an HTML document.
The function runs when the mouse pointer exits the HTML element.
mousedown()
The mousedown() method connects a function to an HTML element that responds when you click on it.
The function activates when you click any mouse button (left, middle, or right) on the HTML element.
mouseup()
The mouseup() function links a special action to an element on a webpage.
The function runs when you let go of the mouse button (left, middle, or right) while the mouse is on the HTML element.
hover()
The hover() method needs two functions and it's like mixing the mouseenter() and mouseleave() methods.
The initial function runs when the mouse goes over the HTML element, and the second one runs when the mouse moves away from the HTML element.
focus()
The focus() method connects a special function to an area in an HTML form.
The function runs when someone clicks on or selects a form field:
blur()
The blur() method connects a function that handles events to an HTML form field.
The function runs when you click outside the form field.
The on() Method
The on() method connects event handlers to chosen elements, allowing one or more handlers to respond to events.
Add a click action to a <p> element:
You can link several actions to a <p> section by following these steps:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("p").click(function() {
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("p").dblclick(function() {
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you double-click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#p1").mouseenter(function() {
alert("You entered p1!");
});
});
</script>
</head>
<body>
<p id="p1">Enter this paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#p1").mouseleave(function() {
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#p1").mousedown(function() {
alert("Mouse down over p1!");
});
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#p1").mouseup(function() {
alert("Mouse up over p1!");
});
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#p1").hover(function() {
alert("You entered p1!");
}, function() {
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
<p id="p1">This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("input").focus(function() {
$(this).css("background-color", "yellow");
});
$("input").blur(function() {
$(this).css("background-color", "green");
});
});
</script>
</head>
<body> Name: <input type="text" name="fullname">
<br> Email: <input type="text" name="email">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("input").focus(function() {
$(this).css("background-color", "yellow");
});
$("input").blur(function() {
$(this).css("background-color", "green");
});
});
</script>
</head>
<body> Name: <input type="text" name="fullname">
<br> Email: <input type="text" name="email">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("p").on("click", function() {
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("p").on({
mouseenter: function() {
$(this).css("background-color", "lightgray");
},
mouseleave: function() {
$(this).css("background-color", "lightblue");
},
click: function() {
$(this).css("background-color", "yellow");
}
});
});
</script>
</head>
<body>
<p>Click or move the mouse pointer over this paragraph.</p>
</body>
</html>