0

I am writing a JavaScript script to perform any action on text selection:

HTML

<div class="text">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem 
Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of
type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release 
of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus
PageMaker including versions of Lorem Ipsum.
</div>

JavaScript

//Trigger event when mouse down on html element having class ".text"
$(".text").on("mousedown", function () {
    //trigger event when mouse up either with text selection or blank
    $(this).one("mouseup", function () {        
       //get selected text
        var $textSelection = window.getSelection().toString();
        //check if text selected or not
        if ( $textSelection.length > 0 ){
            //alert( $textSelection.length )
            alert("You have selected: "+$textSelection );
        }
    });
});
SamV
  • 7,548
  • 4
  • 39
  • 50
Mohan Singh
  • 883
  • 8
  • 18

1 Answers1

0

you got typo there...

$(this).one("mouseup", function () {

this should be

$(this).on("mouseup", function () { 

does it work now ?

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • Guys, i have not missed anything. i have used one in place of on. one introduced in jQuery 1.7 and identical to .on(). Please make me right , if i am wrong – Mohan Singh Jan 09 '14 at 17:52