.blur() JQ Home  <<  Events  <<  .blur()

Blur event handler.

Description

The .blur() method is used to bind an event handler to The JavaScript blur event or trigger that event on the specified element.

  • The blur event is sent to an element when the element loses focus.
  • An element loses focus when the user clicks elsewhere on the page, or uses keyboard commands to move out of the element.

Syntax

Signature Description
.blur( )Trigger the blur JavaScript event on the specified element.
.blur( handler(eventObject) )Bind an event handler to the blur JavaScript event.
.blur( [eventData ,] handler(eventObject) )Bind an event handler to the blur JavaScript event, optionally passing an object of data.

Parameters

Parameter Description Type
handler( eventObject )A function to execute each time the event is triggered.Function
eventDataAn object of data to pass to the event handler.Anything

Return

A jQuery object.

.blur( ) Example  Events  <<  Top

Trigger the blur JavaScript event on the specified element.

  • This signature is a shortcut for .trigger('blur').

In the example below we show a new message in the 'div' element with an id of 'div3' every time the radio button for 'beef pie' loses focus.

When the radio button is pushed we trigger off the blur JavaScript event on the 'div3'. This then fires off the $('#div3').blur(function(){}) code which outputs the message.

Pie Survey

Which pie do you prefer?:

Select a pie shape: 


$(function(){
  $('#beef').blur(function () {
    $('#div1').append('<code>blur</code> JavaScript event triggered.<br>');
  });
  $('#beef').click(function() {
    $('#div1').blur();
  });
});

div1. Some initial text.

.blur( handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the blur JavaScript event.

  • This signature is a shortcut for .on('blur', handler).

In the example below we show a new message in the 'p' element with an id of 'scrollspan1' each time the input element with an id of 'input1' below loses focus.

When the input is typed in, the blur JavaScript event fires off the addText(event) mothod which outputs a message.

What we are doing here is passing across the event object to the function addText(event)method. The data we specify gets tagged onto the event.data property.



$(function(){
  $('#input1').blur(addText);

  function addText(event) {
    $('#scrollspan1').append('blur 1 **JavaScript event triggered**<br>');
  }
});



We will show a message here.


.blur( [eventData ,] handler(eventObject) ) Example  Events  <<  Top

Bind an event handler to the blur JavaScript event, optionally passing an object of data.

  • This signature is a shortcut for .on('blur', handler).

In the example below we show a new message in the 'p' element with an id of 'scrollspan2' each time the input element with an id of 'input2' below loses focus.

When the input field gets blur, the blur JavaScript event fires off the $('#input2').blur({ param1: '#scrollspan2', param2: 'blur 2 ', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText2(event). The map we specify, in our case { param1: '#scrollspan2', param2: 'blur ', param3: '**JavaScript event triggered** ' } gets tagged onto the event.data property. We then access this parameter in the function via event.data.param and use it as part of the appended data.


$(function(){
  $('#input2').blur({ param1: '#scrollspan2', param2: 'blur 2 ', 
                         param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append( event.data.param2 + event.data.param3);
  }
});

We will show a message here.

Related Tutorials

jQuery Advanced Tutorials - Lesson 3 - Form Events