jQuery Event Handling: A Practical Guide for Web Developers

What Are Events?
A website isn't just about displaying information.
Users interact with web pages by clicking buttons, typing on keyboards, moving mice, and more.
This is what we call an event: a reaction triggered when a user performs an action.
Examples of events include:
Button clicks
Mouse over
Mouse out
Keyboard input
Input field selection
Content changes in an input field
Scrolling
Most website functionality revolves around events.
The Basic Structure of Events
In jQuery, events are written like this:
$("#btn").click(function(){
// Code to execute
});
This simply means: when the button with id "btn" is clicked, run the code inside the curly braces.
Preparation for Practice
First, create this HTML:
<h1 id="title">jQuery Events</h1>
<input type="text" id="name">
<button id="btn">Click Me</button>
<div id="box"></div>
Add some CSS for styling:
#box{
width:200px;
height:200px;
background:#4da3ff;
}
click() Event
The most commonly used event.
$("#btn").click(function(){
alert("Button clicked.");
});
An alert appears every time the button is clicked.
This event powers most buttons like sign-up, login, delete, and save actions.
dblclick() Event
Executes when a double-click occurs.
$("#box").dblclick(function(){
alert("Double click");
});
This is separate from regular clicks.
mouseenter()
Executes when the mouse moves over an element.
$("#box").mouseenter(function(){
$(this).css("background","red");
});
The background color changes to red as soon as the mouse hovers over it.
mouseleave()
Executes when the mouse moves away from an element.
$("#box").mouseleave(function(){
$(this).css("background","blue");
});
This event is often used alongside mouseenter().
hover()
A combined function of mouseenter and mouseleave.
$("#box").hover(
function(){
$(this).css("background","red");
},
function(){
$(this).css("background","blue");
}
);
Frequently used for creating menus or card effects.
mousedown() and mouseup()
Executes when the mouse button is pressed:
$("#box").mousedown(function(){
console.log("Pressed");
});
Executes when the mouse button is released:
$("#box").mouseup(function(){
console.log("Released");
});
Commonly used in drag-and-drop features or drawing applications.
focus() Event
Executes when an input field is selected.
$("#name").focus(function(){
$(this).css("background","#ffff99");
});
This can provide visual feedback when a user starts typing.
blur() Event
Executes when an input field loses focus.
$("#name").blur(function(){
$(this).css("background","white");
});
This event is frequently used in sign-up forms.
change() Event
Executes after input content changes and loses focus.
$("#name").change(function(){
alert("Value changed.");
});
This event is also commonly used with select elements.
keyup() Event
Executes when a key is pressed and released.
$("#name").keyup(function(){
console.log($(this).val());
});
This event is widely used for auto-complete search features.
keydown() Event
Executes when a key is pressed down (before release).
$("#name").keydown(function(){
console.log("Key input");
});
This is similar to keyup but executes at a different timing.
submit() Event
Executes before a form is submitted.
$("#myForm").submit(function(){
alert("Form submitted");
});
This event is commonly used in sign-up and login features.
Using 'this' Inside Events
To select the current element that triggered an event, use 'this'.
$("#box").click(function(){
$(this).css("background","green");
});
This changes only the clicked element.
This is a commonly used technique in real-world development.
Registering Multiple Events on One Element
You can register multiple events on the same element:
$("#box")
.mouseenter(function(){
$(this).css("background","red");
})
.mouseleave(function(){
$(this).css("background","blue");
});
Using chaining makes the code cleaner.
Using the on() Function
In real-world development, on() is most commonly used.
$("#btn").on("click",function(){
alert("Clicked");
});
For mouseenter:
$("#box").on("mouseenter",function(){
});
For keyup:
$("#name").on("keyup",function(){
});
All events can be registered in one consistent way.
Registering Multiple Events at Once
$("#box").on({
mouseenter:function(){
$(this).css("background","red");
},
mouseleave:function(){
$(this).css("background","blue");
},
click:function(){
alert("Clicked");
}
});
This method is also frequently used in real-world development.
Real-World Example 1
Change the title when a button is clicked:
$("#btn").click(function(){
$("#title").text("Completed! Event Study");
});
Real-World Example 2
Show the number of characters as you type:
$("#name").keyup(function(){
$("#count").text($(this).val().length);
});
This feature is commonly used in post-writing interfaces.
Real-World Example 3
Create an image zoom effect:
$("#photo").mouseenter(function(){
$(this).css("width","300px");
});
$("#photo").mouseleave(function(){
$(this).css("width","200px");
});
This feature is commonly seen in e-commerce product images.
Event Propagation (Event Bubbling)
HTML elements have parent-child relationships.
Clicking a child element can also trigger events on its parent.
This is called event bubbling.
Understanding this concept will be important when learning about event delegation later.
Common Mistakes for Beginners
Mistaking Event Names (Spelling Errors)
Incorrect example:
clik
Correct example:
click
Make sure to spell event names correctly.
Forgetting the $ Before 'this'
Incorrect example:
this.css(...)
Correct example:
$(this).css(...)
'this' must be converted to a jQuery object before use.
Mixing Up change() and keyup()
keyup executes every time you type.
change executes after typing is complete (when focus is lost).
Select the right event based on your needs.
Practice Assignments
Assignment 1
When the button is clicked, change the title to "Event Study Completed!".
Assignment 2
Change the box to green when the mouse hovers over it and back to original color when moving away.
Assignment 3
Show the current character count as you type in the input field.
Assignment 4
Make an image that zooms when clicked and returns to original size on another click.
Assignment 5
Use focus() and blur() to change the input field's background to yellow when selected and white when not.
Key Takeaways
Events detect user actions on a website.
click() is the most commonly used event.
mouseenter() and mouseleave() are used for mouse effects.
keyup() executes every time you type.
change() executes after typing is complete.
focus() and blur() control input fields.
$(this) refers to the element that triggered the event.
on() is the most commonly used method for registering events in real-world development.
Conclusion
In this lesson, we've learned how to handle events (Event), one of jQuery's core features.
Events allow web pages to react immediately to user actions like button clicks, mouse movements, and keyboard inputs.
Since events are used in almost every part of a website, it's essential to master these methods: click(), hover(), keyup(), focus(), blur(), on().
In the next lesson, we'll learn how to create effects and animations, including fade effects, slide menus, and smooth page transitions to enhance user experience.
FAQ
What is an event in jQuery?
An event in jQuery is a reaction triggered when a user performs an action, such as clicking a button or typing on a keyboard.
Why use the on() function instead of click()?
The on() function allows you to register multiple events in one consistent way and is preferred in real-world development for its flexibility.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [JQuery 강좌] 4강 이벤트(Event) 처리하기