How to save highlighting to a database
        Posted  
        
            by 
                vman
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by vman
        
        
        
        Published on 2011-03-07T02:35:36Z
        Indexed on 
            2011/03/07
            8:10 UTC
        
        
        Read the original article
        Hit count: 389
        
JavaScript
|html
Hi
I am trying to highlight a part of the text on my website. This highlighted text will be saved for the specific user in a database and when the document is opened again it will show the previously highlighted text. I assumed I would be using javascript to highlight the text but I cannot seem to find a way to pinpoint where the word is that I am highlighting.
function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return "";
    return txt;
}
I am using that to get the selection but I cannot figure out where the selection is in the text. The biggest annoyance is when I have duplicates within the line or text so if I were to use search then I would find the first instance and not the one I was looking for.
So the question is : How do you pinpoint a word or a selection in the entire document?
© Stack Overflow or respective owner