2

I tried to get selected text in android on selection.It is not working in touch devices. But it is working fine in normal browsers.

if(!window.Kolich) {
   Kolich = {};
}
Kolich.Selector = {};
// getSelected() was borrowed from CodeToad at
// http://www.codetoad.com/javascript_get_selected_text.asp
Kolich.Selector.getSelected = function() {
   var t = '';
   if(window.getSelection) {
      t = window.getSelection();
   }
   else if(document.getSelection) {
      t = document.getSelection();
   }
   else if(document.selection){
      t =document.selection.createRange().text;
   }
   return t;
}
Kolich.Selector.mouseup = function(e){
   var st = Kolich.Selector.getSelected();
   alert(st);
}

But Android touch selection not working with this code.. please help

RAN
  • 1,443
  • 3
  • 19
  • 30
tom joy
  • 411
  • 8
  • 22
  • Not working how? I imagine by the time `mouseup` fires in a touch browser, any previous selection may have been been destroyed by the touch. Furthermore, for any Android browser and indeed any current browser except IE <= 8, all you need to get the selected text is `window.getSelection().toString()`. That codetoad example is terrible (inconsistent return values: in some browsers it's a Selection object, in others a string) and I'm not sure how it became so popular. – Tim Down Jul 24 '12 at 10:40
  • Mouseup event is not triggering in android text selection – tom joy Jul 24 '12 at 11:03
  • No, I wouldn't expect it to. In recent-ish WebKit browsers (including, I think, the default browser in recent Android versions), there's a `selectionchange` event that fires on `document`. Perhaps that's what you want? – Tim Down Jul 24 '12 at 11:32
  • I want to get the selected text in alert in text selection event(javascript) ...FYI Iam new to android development,Iam UI developer. – tom joy Jul 24 '12 at 11:58

1 Answers1

0

Recent WebKit browsers, including the default browser in recent versions of Android, support a selectionchange event on Document nodes. This event doesn't exist in Opera or Mozilla (but has existed in IE since version 5.5).

Example code:

document.onselectionchange = function() {
    alert( window.getSelection().toString() );
};
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • I wouldn't have just asserted this without testing, but I don't have an Android device handy to test it now. – Tim Down Sep 18 '17 at 09:13