jQuery Mouse, Keyboard and Form Events

In this chapter we see the events which you will use most: mouse, keyboard and form events. Each example is run, and the output is real.

Mouse events

<div id="box">Box</div>
var log = [];
$('#box')
  .on('click', function () { log.push('click'); })
  .on('dblclick', function () { log.push('dblclick'); })
  .on('mouseenter', function () { log.push('mouseenter'); })
  .on('mouseleave', function () { log.push('mouseleave'); });

$('#box').trigger('mouseenter').trigger('click').trigger('mouseleave');
console.log(log.join(', '));
Console output

mouseenter, click, mouseleave

  • mouseenter and mouseleave work only for the element itself. mouseover and mouseout also fire when you move over the children.
  • The method .hover(inHandler, outHandler) is a short way for mouseenter and mouseleave.

Keyboard events

<input id="search" type="text">
<p id="out"></p>
$('#search').on('keyup', function (e) {
  $('#out').text('Key: ' + e.key + ', text: ' + $(this).val());
});

$('#search').val('jQ');
var ev = $.Event('keyup', { key: 'Q' });
$('#search').trigger(ev);
console.log($('#out').text());
Console output

Key: Q, text: jQ

The event object has the key in e.key (like “a”, “Enter” or “ArrowUp”). The old properties keyCode and which are deprecated, so prefer e.key.

Form events

<form id="form">
  <input type="text" id="user" value="">
  <select id="city"><option value="d">Delhi</option><option value="m">Mumbai</option></select>
  <input type="checkbox" id="agree">
  <button type="submit">Send</button>
</form>
<p id="status"></p>
$('#city').on('change', function () {
  $('#status').text('City: ' + $(this).find('option:selected').text());
});
$('#city').val('m').trigger('change');
console.log($('#status').text());

$('#agree').on('change', function () {
  console.log('Checked: ' + this.checked);
});
$('#agree').prop('checked', true).trigger('change');

$('#form').on('submit', function (e) {
  e.preventDefault();
  console.log('Form submitted, user = "' + $('#user').val() + '"');
});
$('#form').trigger('submit');
Console output

City: Mumbai
Checked: true
Form submitted, user = ""

  • change fires when the value of a field is changed and the user leaves it (or at once for select and checkbox).
  • input fires on every change while typing.
  • submit works on the form element. Use e.preventDefault() to stop the normal sending, for example when you use Ajax.

Focus and blur

<input id="email" type="text">
$('#email').on('focus', function () { $(this).addClass('active'); });
$('#email').on('blur', function () { $(this).removeClass('active'); });

$('#email').trigger('focus');
console.log($('#email').hasClass('active'));
$('#email').trigger('blur');
console.log($('#email').hasClass('active'));
Console output

true
false

Note: For text fields, the input event is better than keyup for live search, because it also works when the user pastes with the mouse. Next: Event Delegation.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write jQuery code to hide a paragraph when a button is clicked
Write jQuery code to highlight the clicked list item
Write jQuery code to remove a list item when it is clicked
Write jQuery code to change the image source when a thumbnail is clicked
Write jQuery code to show the text typed in an input field
Write jQuery code to filter a list while typing in a search box
Write jQuery code to count the clicks of a button
Interview Questions & Answers
What is the on() method in jQuery?
What is the difference between click() and on(‘click’) in jQuery?
What is event bubbling in jQuery and how do you stop it?
What is event.preventDefault() in jQuery?
What is event delegation in jQuery and why is it useful?
What is the difference between bind(), live(), delegate() and on() in jQuery?