JS comparing innerHTML to text selection (window.getSelection() /document.selection)

Discussion in 'HTML' started by Andrew Crowe, Sep 13, 2004.

  1. Andrew Crowe

    Andrew Crowe Guest

    Hi,

    I've got a function to get data from a text selection in a users browser:

    function class_getSelection(){
    if (browser.isIE5up) {
    return EpozElement.contentWindow.document.selection;
    } else {
    return EpozElement.contentWindow.window.getSelection();
    };
    }



    I've then got functions that get the parent tag from the selection:


    function class_getSelectionRange(){
    sel = class_getSelection();
    if (browser.isIE5up) {
    return sel.createRange();
    } else {
    return sel.anchorNode;
    };
    }
    function class_getSelectionParent(){
    range = class_getSelectionRange();
    if (browser.isIE5up) {
    return range.parentElement();
    } else {
    return range.parentNode;
    };
    }


    From this I'd like to find out if the user's selection covers the
    entire tag (eg. they've highlited an entire paragraph) or just a
    sentence in the paragraph). I've tried

    selection=class_getSelection();
    object=class_getSelectionParent();
    if(selection==object.innerHTML)

    but unfortunately selection is just the text and innerHTML includes html
    tags / special breaks etc. Is there any way to compare selection and
    innerHTML to work out whether they point to the same thing (eg. the user
    has highlited the entire block).


    Thanks
    Andrew
     
    Andrew Crowe, Sep 13, 2004
    #1
    1. Advertising

  2. Andrew Crowe

    Andrew Crowe Guest

    Re: JS comparing innerHTML to text selection (window.getSelection()/ document.selection)


    > I've got a function to get data from a text selection in a users browser:


    After working on this for a bit what I would really like is a way to
    read the HTML from a users selection, rather then just the text as

    function class_getSelection(){
    if (browser.isIE5up) {
    return EpozElement.contentWindow.document.selection;
    } else {
    return EpozElement.contentWindow.window.getSelection();
    };
    }

    returns.


    I've tried adding innerHTML to the sel.createRange()/sel.anchorNode but
    that doesn't work unfortunatly


    Thanks

    Andrew
     
    Andrew Crowe, Sep 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Allsopp

    Safari window.getSelection()

    John Allsopp, May 13, 2004, in forum: Javascript
    Replies:
    0
    Views:
    172
    John Allsopp
    May 13, 2004
  2. Jason Quinn

    possible to getSelection from AJAX sites?

    Jason Quinn, Apr 18, 2007, in forum: Javascript
    Replies:
    1
    Views:
    99
    Peter Michaux
    Apr 21, 2007
  3. riog
    Replies:
    1
    Views:
    259
  4. riog

    window.getSelection on a textbox

    riog, Oct 17, 2007, in forum: Javascript
    Replies:
    1
    Views:
    130
    Thomas 'PointedEars' Lahn
    Oct 17, 2007
  5. RobG
    Replies:
    2
    Views:
    414
    Dr J R Stockton
    Jul 16, 2011
Loading...

Share This Page