Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy to clipboard text output from database

Tags:

javascript

I have a search form that outputs merchant details (each merchant's name, phone, email address) into a table.
I'm looking to have a copy button next to each one of these fields so users can click on it and copy it to their clipboard (the text gets highlighted when copied). My users will be browsing with IE9 only.

The problem is that there may be more than one set of results, so the script cannot call a specific numbered function like I have done with textarea's below:

function highlightmetasearch01() {
    document.copydata01.text2copy01.select();
     document.copydata01.text2copy01.focus(); 
}
function copymetasearch01() {
    highlightmetasearch01();
    textRange = document.copydata01.text2copy01.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy");
}

function highlightmetasearch02() {
    document.copydata02.text2copy02.select();
    document.copydata02.text2copy02.focus(); 
}
function copymetasearch02() {
    highlightmetasearch02();
    textRange = document.copydata02.text2copy02.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy");
}

HTML:

<textarea name="text2copy01">The first textarea.</textarea>
<br />
<button onclick="copymetasearch01();return false;">COPY</button>

<textarea name="text2copy02">The second textarea.</textarea>
<br />
<button onclick="copymetasearch02();return false;">COPY</button>

I was wondering if this would be possible? ...

<td><span>Name from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>

<td><span>Phone from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>

<td>Other text here that shouldn't be highlighted or copied <span>Email address from DB here</span> <button onclick="<!--copy and highlight text within most recent span tag-->">COPY</button></td>

Or is there a more efficient way to approach this?

like image 520
Zuno Avatar asked Nov 12 '22 16:11

Zuno


1 Answers

This question:

How do I copy to the clipboard in JavaScript?

...contains a fairly long discussion about copying text to the clipboard with JavaScript. The most upvoted (and in my opinion the correct) answer does not actually do the copying, but makes use of a popup to present a text box with the text already selected, allowing the user to simply CTRL+C to copy.

The reasoning behind this is because for a site to control what is on a user's clipboard can be dangerous and undesirable for the user. Understood that here you are getting the user's permission to do so, but still... If you want to take what the answer in above post suggests and apply it to your site, perhaps include a button which automatically selects the text to copy in the field next to it. For information on selecting text in a field see this post: Programmatically selecting partial text in an input field.

like image 71
Levi Botelho Avatar answered Nov 15 '22 06:11

Levi Botelho