jQuery API

.select()

.select( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "select" JavaScript event, or trigger that event on an element.

  • version added: 1.0.select( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.4.3.select( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.select()

This method is a shortcut for .bind('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:

<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:

$('#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:

$('#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.

Examples:

Example: To do something when text in input boxes is selected:

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:blue; }
  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>

    Click and drag the mouse to select text in the inputs.
  </p>
  <input type="text" value="Some text" />
  <input type="text" value="to test on" />

  <div></div>
<script>
    $(":input").select( function () { 
      $("div").text("Something was selected").show().fadeOut(1000); 
    });
</script>

</body>
</html>

Demo:

Example: To trigger the select event on all input elements, try:

$("input").select();

Support and Contributions

Need help with .select() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .select()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Anonymous

    Has anyone experienced issues with Chrome not actually selecting all the text in an input field?

    In the docs above it says “In addition, the default select action on the field will be fired, so the entire text field will be selected.” but Chrome doesn’t seem to do this.

  • bronson

    Apparently this only works for mouse selections? Clicking on a field and hitting Control-A or shift-arrow keys creates a selection but this event isn't fired.

  • bronson

    I take that back — shift-arrow DOES work. It's just Control-A that doesn't. This is in Firefox 3.6.3.

  • http://thestrong.net brandon

    found a solution for select() not working in chrome and safari

    try returning false on mouse up

    $('input').live('focus mouseup', function(e) {
    if (e.type == 'focusin') {
    this.select();
    }

    if (e.type == 'mouseup') {
    return false;
    }
    });

  • elidupuis

    seems to work. thanks brandon. any idea of what default actions we're overriding by returning false?

  • http://ggomez.myopenid.com/ G

    Is there a way to select only a specific portion of the text in the input field?

    For example: if I have a string like http://yourusername.myopenid.com, I would like to only have 'yourusername' portion selected. Is this possible? If so, can you provide a sample, or point me in the right direction?

    Thanks.

  • muddle

    Thanks, works like a charm!

  • nickoooname

    Ever found a sollution?

  • Test

    中国人名共和国

  • this.typeof == 'jQuery.lover'

    Can this be used with <option> tags to select them, instead of doing

    option.attr('selected','selected');

    ?</option>

  • Kailas

    HI
    I am new to Jquery.
    I want select function to work on image.
    is there any way to select image on mouseover event?