Category: javascript
jquery events and event handling
Published on 30 Sep 2026
Explanation
jQuery provides methods such as click(), dblclick(), mouseenter(), and mouseleave() for handling browser events.
Code:
$('#btn').click(function() {
alert('Button clicked');
});
Explanation
The on() method provides a flexible way to attach event handlers to elements.
Code:
$('#btn').on('click', function() {
console.log('Clicked');
});
Explanation
Input events can be handled when the user types into a text field.
Code:
$('#username').on('input', function() {
console.log($(this).val());
});
Explanation
Form submission can be intercepted using the submit event.
Code:
$('#loginForm').on('submit', function(event) {
event.preventDefault();
console.log('Form submitted');
});
Explanation
The this keyword refers to the element that triggered the event, and $(this) converts it into a jQuery object.
Code:
$('.item').on('click', function() {
$(this).addClass('selected');
});