I have a search form that outputs merchant details (each merchant's name, phone, email address). 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 (it's an unpaid work project and as far as I'm aware, the copy-to-clipboard JavaScript only works in IE).

I use similar JavaScript for copying from text areas...

Code:
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"); }

...

<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>
...but the drawback with this is that I'm required to make each copy-to-clipboard function unique to each text area.

I can only output one set block of code for a merchant with PHP, so this prevents me from replicating a function script and incrementing the function/textarea/button numbers.

I was wondering if this would be possible?

Code:
<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 if you have a more efficient way to approach this? I'm very grateful for any help that can be provided or any hints and tips that'd point me in the right direction.