.select()S2C Home « Events « .select()

Select event handler.

Description

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

  • The select event is sent to an element when the element is selected by the user.
  • This event is limited to <input type=text> and <textarea> elements.
  • Setting the location of the insertion point will not trigger the event, only selecting some text will unless triggered manually using the select() signature to manually trigger the event.

Syntax


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

.select( ) Examplego to top of page Top

Trigger the select JavaScript event on the specified element.

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

In the example below we show a new message in the 'div' element with an id of 'div7' when the user clicks anywhere in the input.

When the input is clicked on we trigger off the select JavaScript event on the 'div7'. This then fires off the $('#div7').select(function(){}) code which outputs the message. Selecting some text will also trigger the event.


$(function(){
  $('#div7').select(function () {
    $('#div7').append('<code>select</code> JavaScript event triggered.<br />');
  });
  $('#input4').click(function() {
    $('#div7').select();
  });
});


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


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

Bind an event handler to the select 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 'scrollspan4' each time the input element with an id of 'input5' has some text selected and then loses focus.

When the input field is selected, the select JavaScript event fires off the $('#input5').select({ param1: '#scrollspan4', param2: 'select ', 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: '#scrollspan4', param2: 'select ', 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(){
  $('#input5').select({ param1: '#scrollspan4', param2: 'select ', 
                         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