473,511 Members | 14,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript select text on mouseover

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.

Feb 6 '07 #1
3 15440
On Feb 6, 3:09 pm, trp...@gmail.com wrote:
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?

Feb 8 '07 #2
tr****@gmail.com wrote:
On Feb 6, 3:09 pm, trp...@gmail.com wrote:
>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?

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 <preelement 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/comp.lang.javascript/browse_thread/thread/2b389e61c7b951f2/99b5f1bee9922c39?lnk=gst&q=(doc+%3D+node.ownerDocu ment)+%26%26+(win+%3D+doc.defaultView)&rnum=1&hl=e n#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.

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Feb 8 '07 #3
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 <spantags as the <pretags added an extra
space highlighted at the end of the word, where span does not.
Feb 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1168
by: nadie | last post by:
Hello. I do a page thats change de value in a "select" field. whe you put the mouseOver. showing 1 or 2 in the select field. It works well in IE, but only the bottom area of the map changes the...
4
3816
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
3
2152
by: John Ortt | last post by:
I appologise for reposting this but I have been trying to find a solution all week with no avail and I was hoping a repost might help somebody more knowledgable than myself to spot the message... ...
7
3570
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
13
2320
by: Oliver Hauger | last post by:
Hello, In my html form I show a select-element and if this element is clicked I fill it per JavaScript/DOM with option-elements. I use the following code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD...
2
2803
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
4
5789
by: =?Utf-8?B?c2lMdmVy?= | last post by:
Hhi, I'm working on an asp .net project that have small side panel which 'disallow' long SELECT/dropdown list.. So i could not force them to a certain that shows my longest option, which now...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3219
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.