4

Is there a way to programmatically clear a selection box in Mobile Safari? I tried window.getSelection().removeAllRanges() as suggested by the answers to another Stack Overflow question, Clear Text Selection with JavaScript, and while the Selection object is reset to no selection (type is "None", isCollapsed is true, rangeCount is 0, and the anchorNode/focusNode/baseNode/extentNode are null), the selection box remains on screen:

Screenshot of the 'Clear Text Selection with JavaScript' question on Stack Overflow, showing the selection box still on screen

I also tried window.getSelection().collapse(), but that did not work.

I am testing iOS 7.1 Simulator as well as Mobile Safari on an iPad running iOS 7.1.1.

Community
  • 1
  • 1
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • did u get work around for dis. I need it badly . plz help to remove selection highlight in ipad using javascript/jquery – Khaleel Oct 30 '14 at 07:49
  • 1
    @Khaleel: No, sorry. Though I did find out that this problem has been reported to Apple at least twice. See: http://www.openradar.me/8707236 – Daniel Trebbien Nov 01 '14 at 18:41

3 Answers3

2

This has been a known bug in Mobile Safari since 2010! http://www.openradar.me/8707236

The year is now 2018 and this is still a bug. (iOS 10.2.2)

In areas where it was not needed, removing event.preventDefault() helped.

jfbloom22
  • 804
  • 7
  • 21
0

I was having the exact same problem and in my case what made a difference was removing

event.preventDefault();

from the handler of the "deselection" tap event.

0

One possibility is to quickly add and remove the user-select CSS class of the parent element, to disable and enable the user selection.

For example,

parent = getSelection().anchorNode.parentNode
parent.style.userSelect = "none"
setTimeout(()=>parent.style.userSelect = <default>, 100)
RodericDay
  • 1,266
  • 4
  • 20
  • 35
  • Did this work for you? I just tried it in iOS 11.1 Simulator and it didn't clear the selection either visually or via window.getSelection(). Also, user-select appears to need the `-webkit` prefix in Mobile Safari. – Daniel Trebbien Nov 17 '17 at 19:34
  • It doesn't work the first select + deselect, but it works every time after that. Bizarre behavior, but I don't expect much from Apple. – RodericDay Nov 17 '17 at 21:46