1

This code will return true if the browser supports selectionStart and some text is selected, but if no text is selected it returns false (even on browsers that support it):

   if (el.selectionStart) {

  }

How do you determine if the property is available regardless of whether text happens to be selected?

Thanks

Tim
  • 5,371
  • 3
  • 32
  • 41

2 Answers2

2

Further googling revealed the answer:

 if (el.selectionStart != undefined) {

 }
Tim
  • 5,371
  • 3
  • 32
  • 41
0

Hopefully this will help you. I tested it on an old Android 4.2 (which returns false) and Chrome (which returns true).

function selectionSupport() {
    var input = document.createElement("input");
    input.setAttribute('value', '111');
    input.selectionStart = 1;
    input.selectionEnd = 2;
    return (input.selectionStart === 1 && input.selectionEnd === 2);
}

var selectionIsSupported = selectionSupport();
Nick Taras
  • 696
  • 8
  • 15