Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

[jQuery] How to copy the selected text to a textarea on double click


Hello Friends,

I was in a position where i need to edit some part of div block. I need to select some lines from paragraph and than when we double click on it, i need that text and place it to textarea to edit it. To achieve this, when i double click, it was removing the selection of text. Here is a solution i found for this.

<script type="text/javascript">// <![CDATA[
jQuery(function() {
		jQuery(".importedtext").dblclick( function () { 
		$( '.importedtext' ).mouseup( function( ) { 
			var seltext = getSelectedText();
// Get user selection text on page
function getSelectedText() {
    if (window.getSelection) {
        return window.getSelection();
    else if (document.selection) {
        return document.selection.createRange().text;
    return '';
// ]]></script>
<div class="importedtext">Hi.. this is rakshit Patel.</div>
<input id="hid_session" type="hidden" name="hid_session" value="" />
<input id="hid_session_pre" type="hidden" name="hid_session" value="" />

1 Comment

Leave a Reply

Your email address will not be published.