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

jqury-ui-developer

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.

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

  • loss control jobs
    July 18, 2013 at 11:09 am

    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 *

Sponsors

Sponsors

Recommended

DHgate Computers
DHgate.com - Computers, Cell Phones & more
  • We find everbuying built in php and use css to control the style.

About Author

PHP Freelancer

PHP Freelancer I am PHP Freelancer, Rakshit Patel working as PHP Programmer India. I am here to share all my developing knowledge with you people. For Hire PHP Developer India, contact me.
Follow on twitter: @raxit4u2

Get Free Programming Tutorials:

Become a Fan

Popular Topics

Recent Posts

Most Popular

Recent Comments

Get a Free Quote
$
$
$
Security Question: 3 + 8 = ?
$
 
Listing all pages