jQuery Events

Events let your page react to user actions and browser changes. With jQuery you attach handlers, inspect the event object, stop defaults/propagation, delegate to future elements, trigger custom events, and more.

Quick Reference

Method / Event Description Example
.on(type, [sel], handler)Attach handler; optional selector enables delegation.$(ul).on('click', 'li', fn)
.one(), .off()Run only once; remove handlers.$('#b').one('click', fn), $(doc).off('click')
Mouse: click, mouseenter, mousemove, mouseleavePointer interactions.$('#box').on('mousemove', fn)
Keyboard: keydown, keyupRead e.key/e.which.$('#t').on('keydown', e => ...)
Focus: focus, blurEnter/leave input focus.$('#t').on('focus', fn)
Change & inputinput fires on each keystroke; change on commit.$('#t').on('input', fn)
Form: submitIntercept submission; validate or AJAX.$('#f').on('submit', e => e.preventDefault())
Event object eInfo like e.type, coords, target, keys.$('#box').on('click', e => ...)
e.preventDefault() / e.stopPropagation()Block default / stop bubbling.$('#lnk').on('click', e => e.preventDefault())
Custom: trigger()Fire your own events with data.$(doc).trigger('app:ready', [data])
Ready: $(fn)Run when DOM is ready.$(function(){ ... })
Window: scroll, resizeReact to viewport changes (throttle).$(win).on('scroll resize', fn)
Tip: Prefer .on() with delegation for dynamic lists/grids ($(parent).on('click', 'li.item', fn)). It binds once on the parent and works for future items.

1) Click


$('#btn').on('click', function(){
  console.log('clicked');
});

2) Mouse Events


$('#box').on('mouseenter mousemove mouseleave', function(e){ ... });

3) Keyboard


$('#t').on('keydown keyup', function(e){ // e.key });

4) Focus / Blur


$('#t').on('focus blur', function(){ ... });

5) change vs input


$('#t').on('input change', function(){ ... });

6) Form submit


$('#f').on('submit', function(e){ e.preventDefault(); ... });

7) Event Object + preventDefault / stopPropagation


$('#lnk').on('click', function(e){
  e.preventDefault(); // don't follow link
});

8) Event Delegation


$('#list').on('click', 'li.item', function(){ ... });

9) on() / one() / off()


$('#b').one('click', function(){ ... }); // once
$(document).on('click', fn);
$(document).off('click', fn);

10) Custom Events & trigger()


$(document).on('app:ready', function(e, data){ ... });
$(document).trigger('app:ready', [{ user: 'Sonu' }]);

11) DOM Ready & Window Events


$(function(){ // DOM ready ... });
$(window).on('scroll resize', function(){ ... });
Debugging: Log the event object and this inside handlers to understand context. For performance, throttle high-frequency events like scroll and resize.