javascript select text on mouseover

T

trpost

I want to be able to select a word when I highlight over any part of
the word. When I say select the word, I want it to be highlighted as
if I left clicked my mouse and dragged the cursor along the word. I
want to do this so a user when putting the mouse over the word can
quickly hit <ctrl>c to copy the word, without having to manually
highlight the word.

This will be used multiple times in the script for specific words, not
for every word in the script. For example:

User Password
------- ---------------
admin adminUser
dba sysDbAPa$$

So in my above example when mousing over either of the passwords, the
entire password would be highlighted so I can quickly copy it.
 
T

trpost

I want to be able to select a word when I highlight over any part of
the word. When I say select the word, I want it to be highlighted as
if I left clicked my mouse and dragged the cursor along the word. I
want to do this so a user when putting the mouse over the word can
quickly hit <ctrl>c to copy the word, without having to manually
highlight the word.

This will be used multiple times in the script for specific words, not
for every word in the script. For example:

User Password
------- ---------------
admin adminUser
dba sysDbAPa$$

So in my above example when mousing over either of the passwords, the
entire password would be highlighted so I can quickly copy it.

Anyone??? Is this even possible, or has anyone seen this done in the
past?
 
P

pcx99

Anyone??? Is this even possible, or has anyone seen this done in the
past?


It's possible. Your problem is that it's hideously complex and so
doesn't fall into a category that will get an easy answer on the newsgroup.

To give you a starting point here is a script written by Martin Honnen
which will select the contents of any HTML element you pass it. Sample
usage is this..

<pre onClick="selectNode(this)">
blah blah blah
</pre>

When you click on the <pre> element it will be selected for easy
clipping into the clipboard.

function selectNode (node) {
//This is a third party function written by Martin Honnen
//In comp.lang.javascript

//http://groups-beta.google.com/group...oc.defaultView)&rnum=1&hl=en#99b5f1bee9922c39
var selection, range, doc, win;
if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof
win.getSelection != 'undefined' && typeof doc.createRange != 'undefined'
&& (selection = window.getSelection()) && typeof
selection.removeAllRanges != 'undefined') {
range = doc.createRange();
range.selectNode(node);
selection.removeAllRanges();
selection.addRange(range);
} else if (document.body && typeof document.body.createTextRange !=
'undefined' && (range = document.body.createTextRange())) {
range.moveToElementText(node);
range.select();
}
}

That should at least get you started if you want to pursue it from there.
 
T

trpost

Thanks! That helped me! Here is how I ended up using it:

<span onmouseover="selectNode(this)";
OnMouseOut="clearSelection(this);">blah</span><br>
<span onmouseover="selectNode(this)";
OnMouseOut="clearSelection(this);">blah1</span><br>
<span onmouseover="selectNode(this)";
OnMouseOut="clearSelection(this);">blah2</span><br>
<span onmouseover="selectNode(this)";
OnMouseOut="clearSelection(this);">blah3</span><br>

<script language=javascript>
function selectNode (node)
{
var selection, range, doc, win;

if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof
win.getSelection != 'undefined' && typeof doc.createRange !=
'undefined' && (selection = window.getSelection()) && typeof
selection.removeAllRanges != 'undefined')
{
range = doc.createRange();
range.selectNode(node);
selection.removeAllRanges();
selection.addRange(range);
}
else if (document.body && typeof document.body.createTextRange !=
'undefined' && (range = document.body.createTextRange()))
{
range.moveToElementText(node);
range.select();
}

}


function clearSelection ()
{
if (document.selection)
document.selection.empty();
else if (window.getSelection)
window.getSelection().removeAllRanges();
}


</script>

So I created an additional function to clear the selection on
OnMouseOut and am using <span> tags as the <pre> tags added an extra
space highlighted at the end of the word, where span does not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top