selectedtext - how to get selected text, but can I get surrounding context in javascript? -
i able grab text user has selected on web page, using code:
function getselected() { var userselection; if (window.getselection) { selection = window.getselection(); } else if (document.selection) { selection = document.selection.createrange(); } }
is posible me words around selected word.
take these sentences example: "if need framglubble zartbox, should buy red widget. otherwise can buy blue widget , save money."
my code tell me if person has selected word "widget". i'd know if selection after "red" or "blue". possible? i've been scouring internet advice, , i'm having trouble finding answer.
thank help
i have written quick script can identify part before selection , after selection inside same div element.
however if same div contains same word more 1 time , select word, current code wrote can't identify if it's first or second selected word bottom line not answer needs.
anyway, can see/copy/test code here: http://jsfiddle.net/kvhxj/ select , see alert appears.
if it's enough needs after great, accept answer , move on... otherwise need know: can assume user select whole words only, 1 word only? if answer yes have idea how go around this.
Comments
Post a Comment