0

I'm trying to get the non selected text in an Input Text element using IE8, I tried this and was really helpfully, but in the case of the text wasn't selected the function returns the index of start and end of the string.

How can I get the non selected text of the input text element or how can I know when there isn't text selected?

Using jQuery would be helpfully too.

Community
  • 1
  • 1
Sergio Flores
  • 5,231
  • 5
  • 37
  • 60

1 Answers1

0

If I've understood your question correctly, you'll need something like this:

function getNonSelectedText(){
    var elem=document.getElementById('input');
    var selection=document.selection;
    var range=selection.createRange();
    if(range.boundingWidth<1){
        return elem.value;
    }
    var rangePre=range.duplicate();
    rangePre.moveToElementText(range.parentElement());// For <textarea>
    // rangePre.expand('textedit'); // For <input type=text>
    var rangePost=rangePre.duplicate();
    rangePre.setEndPoint('EndToStart',range);
    rangePost.setEndPoint('StartToEnd',range);
    return rangePre.text+rangePost.text;
}
Teemu
  • 22,918
  • 7
  • 53
  • 106