Programmatically selecting partial text in an input field
Asked Answered
R

3

51

How can I programmatically select a specific range of text in an HTML input field? (I don't want to select the entire field, just a subset)

Also, how can I determine the currently selected range in a field?

Retinoscope answered 14/3, 2009 at 20:3 Comment(2)
You need to explain a little more about what you are trying to do.Lengthen
Oh, you want to select individual Chars. :/Siobhansion
E
65

Here's how to select a portion of a text box (range select) and get the selected text:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> Test </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
  window.onload = function() {
    var message = document.getElementById('message');
    // Select a portion of text
    createSelection(message, 0, 5);
    // get the selected portion of text
    var selectedText = message.value.substring(message.selectionStart, message.selectionEnd);
    alert(selectedText);
  };

  function createSelection(field, start, end) {
    if( field.createTextRange ) {
      var selRange = field.createTextRange();
      selRange.collapse(true);
      selRange.moveStart('character', start);
      selRange.moveEnd('character', end);
      selRange.select();
      field.focus();
    } else if( field.setSelectionRange ) {
      field.focus();
      field.setSelectionRange(start, end);
    } else if( typeof field.selectionStart != 'undefined' ) {
      field.selectionStart = start;
      field.selectionEnd = end;
      field.focus();
    }
  }
</script>
</head>
<body>
<input type="text" name="message" id="message" value="Hello World" />
</body>
</html>

Demo

Ebberta answered 14/3, 2009 at 20:27 Comment(1)
There are 2 bugs here. 1. you need to focus before calling setSelectionRange or nothing will be selected. 2. the 3rd if statement should be else if( typeof field.selectionStart != 'undefined' ) because field.selectionStart can evaluate to 0 which is falsyProclamation
T
14

Small correction. It seems that IE moveEnd() method moves incrementally so selRange.moveEnd('character', end) should be replaced with selRange.moveEnd('character', end-start):

function createSelection(field, start, end) {
    if( field.createTextRange ) {
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart('character', start);
        selRange.moveEnd('character', end-start);
        selRange.select();
    } else if( field.setSelectionRange ) {
        field.setSelectionRange(start, end);
    } else if( field.selectionStart ) {
        field.selectionStart = start;
        field.selectionEnd = end;
    }
    field.focus();
} 
Tweeze answered 28/3, 2009 at 0:30 Comment(0)
K
4

Thank you! I wanna share my function now, it's used in Ajaxel CMS along with Instant messenger

,wrapText:function(o, ot, ct) {
    var s = o[0].selectionStart; 
    var e = o[0].selectionEnd;
    o.val(o.val().substring(0, s)+ot+o.val().substring(s,e)+ct+o.val().substring(e, o.val().length)); 
    if (o[0].createTextRange){
        var sr = o[0].createTextRange(); 
        sr.collapse(true); 
        sr.moveStart('character', s); 
        sr.moveEnd('character',e-s+ot.length+ct.length); 
        sr.select();
    }
    else if(o[0].setSelectionRange){ 
        o[0].setSelectionRange(s,e+ot.length+ct.length); 
    }
    else if(o[0].selectionStart){ 
        o[0].selectionStart=s; 
        o[0].selectionEnd=e+ot.length+ct.length; 
    }
}
Kitchenette answered 29/12, 2011 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.