Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  137] [ 2]  / answers: 1 / hits: 30750  / 12 Years ago, fri, october 19, 2012, 12:00:00

I heard that you can't copy text (in the browser) without using something like Flash; so, is there a way to select text by using an anchor and JavaScript or jQuery.



<p>Text to be copied</p>

<a>Copy Text Above</a>

More From » jquery

 Answers
10

Given the following example html:



<div class=announcementInfoText>
<p class=copyToClipboard>
<a id=selectAll>Select All Text</a>
</p>
<textarea ID=description class=announcementTextArea>This is some sample text that I want to be select to copy to the clipboard</textarea>
</div>


you can select the text within the textarea with the following jQuery:



$(#selectAll).click(function () {
$(this).parents(.announcementInfoText).children(textarea).select();
});


Now that the text This is some sample text that I want to be select to copy to the clipboard is selected, you can simply hit Ctrl+C and the text is copied to the clipboard.


[#82466] Thursday, October 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deannaalysonl

Total Points: 703
Total Questions: 101
Total Answers: 115

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;