javascript - Jquery - how to copy selected text from div tag? - Stack Overflow

lets say I have two DIVS in the page. #news div and #imp div respectively.<div id="news"&g

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.

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.

Share edited Jan 4, 2015 at 19:39 jmore009 12.9k1 gold badge21 silver badges34 bronze badges asked Jan 4, 2015 at 19:30 MorezMorez 2,2065 gold badges40 silver badges49 bronze badges 2
  • by selected do you mean when someone highlights a word? – jmore009 Commented Jan 4, 2015 at 19:40
  • @jmore009 Thanks for editing. yeah exactly. highlighted word get pasted in other div #imp. – Morez Commented Jan 4, 2015 at 19:43
Add a ment  | 

3 Answers 3

Reset to default 6

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./xabije/edit?html,js,output

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

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

and use jquery to insert to the div imp

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098131a4611106.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信