PDA

View Full Version : If there something in the DOM or javascript to know characters selected?



aaava
08-17-2005, 05:51 PM
Hi folks:

I'm working on a site for writers, and am trying to figure out a way to know the characters/words selected by a user. I'm not sure whether to include this in the javascript forum or the css forum...

For instance, say someone is reading a page in a story online in a browser (NOT inside a textarea control, though if it's possible there, then great. I'm talking just clicking and selecting part of a web page. They click the mouse over a character, drag the mouse until a whole word is selected, and then mouseup. A word (or phrase) has just been selected, and I'd like to do some ajax-type things w/that selection. This has to be multi-browser, though I don't care about older browsers too much, and fahgedabout Mac IE 5.2.3 ;-)

Does anyone have any idea if this is possible, and if so, how to do it? I know I can track a mousedown and mouse up event...but I've never seen any event that tracks a selection, though, of course, each browser has a 'cut/copy' capability, so the functionality is there *somewhere*. Thanks for any help. I've got a bald spot trying to figure this thing out.

mwinter
08-17-2005, 07:13 PM
I'm working on a site for writers, and am trying to figure out a way to know the characters/words selected by a user. I'm not sure whether to include this in the javascript forum or the css forum...CSS suggests how to present a document. It has nothing to do with text selection.


Does anyone have any idea if this is possible, and if so, how to do it?QuirksMode (http://www.quirksmode.org/) has an article on obtaining the contents of a text selection (http://www.quirksmode.org/js/selected.html). Note that Opera's support for text selection seems to start from Opera 7, not 7.50 as the article suggests. However, if you use a click/mousedown event to run the code, Opera will return an empty string as the click will deselect the text.


of course, each browser has a 'cut/copy' capability, so the functionality is there *somewhere*.That's rather flawed logic. For instance, you could copy text from a document in Opera 6, but there's no way to determine what has been selected.

Mike

aaava
08-17-2005, 07:32 PM
I appreciate you pointing me in the right direction. My thanks would by unrestricted if you hadn't insisted on the semantics lesson. Geez. An answer in itself is very helpful. The rest? Unnecessary.