.select()


.select( handler )Возвращает: jQuery

Описание: Устанавливает обработчик выделения текста внутри элемента или , либо, запускает это событие.

  • Добавлен в версии: 1.0.select( handler )

    • handler
      Тип: Function( Event eventObject )
      A function to execute each time the event is triggered.
  • Добавлен в версии: 1.4.3.select( [eventData ], handler )

    • eventData
      Тип: Anything
      An object containing data that will be passed to the event handler.
    • handler
      Тип: Function( Event eventObject )
      A function to execute each time the event is triggered.
  • Добавлен в версии: 1.0.select()

    • This signature does not accept any arguments.

This method is a shortcut for .on( "select", handler ) in the first two variations, and .trigger( "select" ) in the third.

The select event is sent to an element when the user makes a text selection inside it. This event is limited to <input type="text"> fields and <textarea> boxes.

For example, consider the HTML:

1
2
3
4
5
6
<form>
<input id="target" type="text" value="Hello there">
</form>
<div id="other">
Trigger the handler
</div>

The event handler can be bound to the text input:

1
2
3
$( "#target" ).select(function() {
alert( "Handler for .select() called." );
});

Now when any portion of the text is selected, the alert is displayed. Merely setting the location of the insertion point will not trigger the event. To trigger the event manually, apply .select() without an argument:

1
2
3
$( "#other").click(function() {
$( "#target" ).select();
});

After this code executes, clicks on the Trigger button will also alert the message:

Handler for .select() called.

In addition, the default select action on the field will be fired, so the entire text field will be selected.

The method for retrieving the current selected text differs from one browser to another. A number of jQuery plug-ins offer cross-platform solutions.

Дополнительные замечания:

  • As the .select() method is just a shorthand for .on( "select", handler ), detaching is possible using .off( "select" ).

Примеры использования