3

I need to trigger event when user selects text in android touch device.In web browsers am able to trigger event by checking on each mouse up event whether window.getselection is null. Touch select text event not triggering.

document.addEventListener('touchend', function(event)
{
    if( window.getSelection){
        t = window.getSelection().toString();
        alert(t);
    }
});

I tried in touchend event.But when user selects text event not triggering.

tom joy
  • 411
  • 8
  • 22

2 Answers2

0

Why not try a jQuery solution using the .change() function? First you add the jQuery library immediately above your included JavaScript file declaration, then you've got jQuery capability in the document. Next read up on .change() and try something like:

  $('#touchend').change(function(){
      if(your condition){var $t = window.getSelection().toString();}
  });

or whatever JavaScript code you want in the function. jQuery is simply a JavaScript library, so you can use it freely with your JavaScript. It's a gamechanger!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • I appreciate the edit, but was merely trying to add a link to direct the asker to jquery's .change() documentation. – The Sethness Oct 28 '13 at 22:57
0

You actually want to listen to the event selectionchange.

Note that it will fire for both creating a selection and clearing the selection so when you get that event, you want to query the selection with window.getSelection() and check the status of isCollapsed.

Mikko Rantalainen
  • 14,132
  • 10
  • 74
  • 112