.blur()S2C 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( [eventData], handler(eventObject) )Bind an event handler to the blur JavaScript event, optionally passing an object of data.

Parameters


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

Return

A jQuery object.

.blur( ) Examplego to top of page 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 () {
    $('#div3').append('<code>blur</code> JavaScript event triggered.<br />');
  });
  $('#beef').click(function() {
    $('#div3').blur();
  });
});

div3. Some initial text.



.blur( [eventData], handler(eventObject) ) Examplego to top of page Top


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

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

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 ', 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 ', 
                         param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append(event.data.param2 + ''<code>' + event.data.param3+ ''</code>');
  }
});


We will show a message here.

go to home page Homepage go to top of page Top