Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying link text to clipboard

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
I'm trying to build the following:
- link on page says: Bob
- click on link, Bob's email address bob[at]bob.com is copied into the clipboard, ready to be pasted elsewhere.

I'll have lots of entries on one page (it's an HTML phone book) so I don't want a complex function in every entry.

What I have so far is this:
<a href=&quot;bob[at]bob.com&quot;>Bob</a>
I right-click and 'Copy Shortcut'. But what this copies is:
file://c:/<full filepath>/bob[at]bob.com

Oddly, different combinations of letters produce different results:
Code:
mailto:bob[at]bob.com  =                     mailto:bob[at]bob.com
    mm:bob[at]bob.com  =                         mm:bob[at]bob.com
     m:bob[at]bob.com  =                  file:///m:bob[at]bob.com
      :bob[at]bob.com  = file://c:/<full filepath>/:bob[at]bob.com
       bob[at]bob.com  =  file://c:/<full filepath>/bob[at]bob.com
None of which are helpful.

I have looked at scripts that copy *selected* text into the clipboard, but that's not quite what I'm trying to do. I want to click on one piece of text, but have something else copied.

Advice?

 
Instead of putting the email in the href element, put in in the value element as follows:

<html>
<body>
<form name=blahForm>
<a href=&quot;#&quot; value=&quot;bob@bob.com&quot; onclick=&quot;alert(this.value)&quot;>Bob</a>
</form>
</body>
</html>

then modify this code at dynamic drive to copy it to the clipboard:


-kaht

banghead.gif
 
I don't know how I would convert this to work with plain live text instead of form/field text. I have no textarea to pass as an object.
 
<a href=&quot;#&quot; value=&quot;bob@bob.com&quot; onclick=&quot;alert(this.value)&quot; name=&quot;blahAnchor&quot;>Bob</a>
<textarea name=&quot;blahTextArea&quot;></textarea>

you'd access the values the same for each tag.

alert(formName.blahAnchor.value);
alert(formName.blahTextArea.value);

you just have to give your anchor a name or id to access it's value (or use some kind of form.elements approach, naming it would be easier)

-kaht

banghead.gif
 
It's not about getting at the anchor's properties. As far as I can tell, CreateTextRange() doesn't work on anchors (or likely any object where you can't select a block of text). I can't copy it if I don't have it selected.
 
Using the script from dynamic drive (modified slightly) I have given you a working example. It uses a hidden text field to achieve what you're trying to do.
Code:
<html>
<head>
<style>
.highlighttext{
background-color:yellow;
font-weight:bold;
}
</style>

<script language=&quot;Javascript&quot;>
<!--

/*
Select and Copy form element script- By Dynamicdrive.com
For full source, Terms of service, and 100s DTHML scripts
Visit [URL unfurl="true"]http://www.dynamicdrive.com[/URL]
*/

//specify whether contents should be auto copied to clipboard (memory)
//Applies only to IE 4+
//0=no, 1=yes
var copytoclip=1

function HighlightAll(theField, str) {
test.select1.style.visibility = &quot;visible&quot;;
var tempval=eval(&quot;document.&quot;+theField)
tempval.value = str;
tempval.focus()
tempval.select()
if (document.all&&copytoclip==1){
therange=tempval.createTextRange()
therange.execCommand(&quot;Copy&quot;)
test.select1.style.visibility = &quot;hidden&quot;;
window.status=&quot;Contents highlighted and copied to clipboard!&quot;
setTimeout(&quot;window.status=''&quot;,1800)
}
}
//-->
</script>

</head>
<body>
<form name=test>
<a href=&quot;#&quot; class=&quot;highlighttext&quot; value=&quot;bob@bob.com&quot; onclick=&quot;javascript:HighlightAll('test.select1', this.value)&quot;>Select All</a><br>
<input type=text name=&quot;select1&quot;>
<script language=&quot;Javascript&quot;>
test.select1.style.visibility = &quot;hidden&quot;;
</script>

</form>
</body>
</html>

-kaht

banghead.gif
 
Here's my effort:
Code:
function copyLinkText(aLink){
 // creates a temporary textarea to build the text range
 var tempText = document.createElement(&quot;textarea&quot;);
 tempText.innerText = aLink.innerText;
 
 var rng = tempText.createTextRange()

 // copy the text to the clipboard
 rng.execCommand(&quot;copy&quot;);
 alert(&quot;The Text '&quot; + rng.text + &quot;' has been copied &quot; +
       &quot;to the clipboard\n&quot; +
       &quot; and can be pasted into another application.&quot;);
}

Called directly from the link as such:
Code:
<a href=&quot;mailto:bob@bob.com&quot; 
   onclick=&quot;copyLinkText(this)&quot;>bob@bob.com</a>

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

AU.gif
Check out Tek-Tips Australia New Zealand forum1155
NZ.gif
 
Dwarfthrower: that does not work.

Note that you have put the _link_ 'bob@bob.com' the same as the _text_ 'bob@bob.com'. Your method copied the text, not the link.

If you make the text simply 'Bob' (as required), you'll see that it's still copying 'Bob', not 'bob@bob.com'.



Kaht: Teehee. The posting engine has translated what it thought was an HTML extended copyright character. It changed document.all&&copytoclip into document.all&(copyright)toclip


Anyway, your method works - but what a workaround! An instant appearing/disappearing text box...
I would surely have thrown in the towel before this, assuming that this was badly hacked code on my part.

Thanks!
 
>> Dwarfthrower: that does not work.

>> Note that you have put the _link_ 'bob@bob.com' the
>> same as the _text_ 'bob@bob.com'. Your method copied
>> the text, not the link.


Then change the text box to pick up the link and not the text:
[tt]
function copyLinkText(aLink){
// creates a temporary textarea to build the text range
var tempText = document.createElement(&quot;textarea&quot;);
tempText.innerText = aLink.innerText;
tempText.innerText = aLink.href;
var rng = tempText.createTextRange()

// copy the text to the clipboard
rng.execCommand(&quot;copy&quot;);
alert(&quot;The Text '&quot; + rng.text + &quot;' has been copied &quot; +
&quot;to the clipboard\n&quot; +
&quot; and can be pasted into another application.&quot;);
}
[/tt]


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

AU.gif
Check out Tek-Tips Australia New Zealand forum1155
NZ.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top