Morez
Morez

Reputation: 2196

Jquery - how to copy selected text from div tag?

lets say I have two DIVS in the page. #news div and #imp div respectively.

<div id="news">
Apple is facing a lawsuit for not telling users about the amount of memory required by an upgrade       its flagship operating system.
</div>

<div id="imp">
 <!-- Empty -->
</div>

Now my requirement is if I select only 'Apple' from the whole sentence then that selected portion gets copied and pasted into div #imp. And if copied, other portions then get appended.

Upvotes: 1

Views: 4676

Answers (3)

Scott Duke
Scott Duke

Reputation: 91

Could you do something like this?

$( document ).ready(function() {
  $('#news').mouseup(function (e){
    text = window.getSelection().toString();
    $('#imp').append(text);
  });
});

Hope this helps.

JS Bin: http://jsbin.com/xabije/edit?html,js,output

Upvotes: 6

jmore009
jmore009

Reputation: 12923

Scott Duke beat me to it but the point was to append so I changed html to append and added a space so the words don't jumble together:

$('#news').mouseup(function (){
   var selectedText = window.getSelection().toString();
   $('#imp').append(" "+ selectedText)
});

FIDDLE

Upvotes: 0

lazyboy
lazyboy

Reputation: 311

I would copy the marked text, using this library, into clipboard

https://code.google.com/p/liveclipboard-jquery/

and use jquery to insert to the div imp

Upvotes: 0

Related Questions