0

Apologies if this is a total noobish question. I'm new to JS and not sure if the following can be accomplished.

Does DOM provide any API that allows us to access the offset of a selected text string? I need to highlight a text string and save that so that it is accessible next time when I login in. I was googling around and the impression I'm getting is that there are cross-browser compatibility issues even if you are able to do it. I'm hoping that there is some way out? Any pointers would be much appreciated.

RTores
  • 85
  • 1
  • 1
  • 4
  • 2
    Please post some example code so we can understand your question better. – David Hellsing Sep 24 '12 at 15:03
  • Can you elaborate on 'save'? Are you going to store the string(s) highlighted on the server? – Phil H Sep 24 '12 at 15:04
  • Why not just add a class around the text string to highlight with CSS? – jsweazy Sep 24 '12 at 15:06
  • Possible duplicate? http://stackoverflow.com/questions/5379120/jquery-get-the-highlighted-text – Ortiga Sep 24 '12 at 15:06
  • Have a look at this question: http://stackoverflow.com/questions/9064363/javascript-text-selection-events – Phil H Sep 24 '12 at 15:06
  • Phil - This is what I was thinking. If can get an offset to the start and the end of the string I will save the begin_offset and end_offset along with the #id of the that particular text node. So next time when I come in I will get those values back and highlight that text. – RTores Sep 24 '12 at 15:09

3 Answers3

1

You could use 'selectionStart' and 'selctionEnd' of the element. Something like

document.getElementById("id").selectionStart
document.getElementById("id").selectionEnd
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Have a look at the Rangy Serializer Module.

It'll allow you to serialize a selection returning a string like 0/9/3/2:13,0/9/3/2:44{df7c63db}.

In your case, you could save the serial to local storage and restore the selection using the serial on the next login.

Brian Ustas
  • 62,713
  • 3
  • 28
  • 21
0

I am still not sure of what you want but I think this blog would help

http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html

Rahul
  • 1,549
  • 3
  • 17
  • 35
  • I don't have code to show because I don't know where exactly to start on this :-) Maybe if I explain the use case that might help. I have a set of HTML documents which the user has access to after he logs in. User will read the documents and highlight text that interests him. Once highlighted the text will be saved. So the next time the user logs in the highlighting will be visible for the selection he/she made earlier. The link you provided doesn't address this as far as I can understand – RTores Sep 24 '12 at 15:23
  • You can use the above blog to get the data , once you have the selected data you can have various options to store it, local storage etc.I suggest using the .data plugin of Jquery, only for the simplicity of it. I hope this helps if not keep asking questions to clarify – Rahul Sep 24 '12 at 15:30
  • Thanks but I would like to make a correction to the comment I made. "Once highlighted the text will be saved". I meant - once highlighted the position/offset of the text will be saved – RTores Sep 24 '12 at 15:38