Hackforge Academy

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');
});

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community