3

The following JavaScript code can change user's selection range in UIWevView but iOS selection remains unchanged. How can I synchronize both selections, so when the app changes selection using JavaScript, iOS selection changes as well. Is it possible?

var sel = window.getSelection();

var range = document.createRange();
range.setStart(sel.anchorNode,sel.anchorOffset + 1); // it removes the first letter from a selected range
range.setEnd(sel.focusNode,sel.focusOffset);

sel.removeAllRanges();
sel.addRange(range);
walker
  • 140
  • 1
  • 8

1 Answers1

1

You can change the Selection object as you can in any other browser but iOS does not update the visible selection unless the selection is already visible, for reasons unknown to me. There's no way round this as far as I know.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 2
    If it's not possible, maybe I will try to highlight selection using css... not the best solution but better than nothing to make a new selection visible. – walker Feb 15 '12 at 16:29
  • @user828845: `document.execCommand()` will do that for you. See http://stackoverflow.com/a/3224513/96100 – Tim Down Feb 15 '12 at 17:20
  • Thanks! I will try. By the way, I tried ::selection (css) to highlight selected text but unfortunately it doesn't work in Mobile Safari :( – walker Feb 15 '12 at 21:46
  • Setting contentEditable to true allows for more robust text selection. – mattsven Jul 03 '13 at 16:28