Monday , 22 December 2014
Home » JavaScript » [jQuery] How to copy the selected text to a textarea on double click
[jQuery] How to copy the selected text to a textarea on double click

[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 () { 
			alert(jQuery("#hid_session_pre").val()); 
		});
 
		$( '.importedtext' ).mouseup( function( ) { 
			var seltext = getSelectedText();
			jQuery("#hid_session_pre").val(jQuery("#hid_session").val());
			jQuery("#hid_session").val(seltext); 
		});
});
 
// 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="" />

About Rakshit Patel

I am PHP Freelancer, Rakshit Patel AKA WordPress Freelancer working as PHP Programmer India. I am here to share all my developing knowledge, quick coding techniques, quick solutions to save your time and help you guys for any issue or bug fixing. If you have any PHP, WordPress, Magento related work, please contact me. I am interested for the work.

One comment

  1. This blog was… how do you say it? Relevant!
    ! Finally I’ve found something that helped me. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>