.focusin()S2C Home « Events « .focusin()

Focusin event handler.

Description

The .focusin() method is used to bind an event handler to the JavaScript focusin event.

  • The focusin() event is sent to an element when it, or any descendant, gains focus. This is distinct from the focus event in that it supports detection of the focus event on parent elements, ergo it supports event bubbling.
  • The focusin() event is often used in tandem with the .focusout() event.

Syntax

Signature Description
.focusin( )Trigger the focusin JavaScript event on the specified element.
.focusin( handler(eventObject) )Bind an event handler to the focusin JavaScript event.
.focusin( [eventData ,] handler(eventObject) )Bind an event handler to the focusin 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.


.focusin( ) Example Top

Trigger the focusin JavaScript event on the specified element.

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

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

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


Pie Survey

Which pie do you prefer?:

Select a pie shape: 

div1. Some initial text.



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

  $('#fish').click(function() {
    $('#div1').focusin();
  });
});




.focusin( handler(eventObject) ) Example Top

Bind an event handler to the focusin JavaScript event.

  • This signature is a shortcut for .on('focusin', 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 gains focus.

When the input is typed in, the focusin 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').focusin(addText);

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



We will show a message here.



.focusin( [eventData ,] handler(eventObject) ) Example Top

Bind an event handler to the focusin JavaScript event, optionally passing a map of data.

  • This signature is a shortcut for .on('focusin', 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' gains focus.

When the element gains focus, the focusin JavaScript event fires off the $('#input2').focusin({ param1: '#scrollspan2', param2: 'focusin 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: 'focusin 2 ', 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').focusin({ param1: '#scrollspan2', param2: 'focusin 2 ', 
                          param3: '**JavaScript event triggered**<br>' }, addText2);
  function addText2(event) {
    $(event.data.param1).append(event.data.param2 + event.data.param3);
  }
});

We will show a message here.