PDA

View Full Version : onselect and oncopy -- need to copy only what is highlighted, not all of the text


htmlcssnewbie
09-22-2008, 09:13 AM
Say I have an input textbox. When I highlight said textbox with my mouse, it will trigger an onselect event. Now, what this onselect event should do is copy the text that was highlighted to the clipboard. Better yet, becasue copying to the clipboard works only in iE, iirc, copy the text to a global javascrpt variable. Either way, I need to understand how one would know how much of the text the user highlighted.

Edit: I have figured out how to select the text I want and copy or delete it as I wish. Now what I wish to do is figure out the caret position. I read one article that said, for non-gecko-based browsers (read: IE), you can insert a delimiter and use that to find the caret but it seems overly complex and unnecessary. There must be a simpler way, one would think.

Thank you.

dntget0wned
09-25-2008, 03:13 PM
can you be a little more specific
you want to know where the caret position is?
are you expecting there to be a ^ in your text?
do you want to know the position of the caret in just the highlighted text or the input box?

dntget0wned
09-25-2008, 03:27 PM
This Javascript function will locate a specific character in a string

// string is string to be searched
// char is character to be located
function findChar(str,char){
txt=str.split('');
var i=0;
for(i=0;i<txt.length;i++)if(txt[i]==char)break;
return i;
};