GetSelection () and insertNode - Selecting text in Javascript

Does anyone know how to set browser selection to a new / independently created range? I understand how to get the selection of text from the browser and I understand how to create a range, but I don't know how to tell the browser to change the selection to the range I created. I would have thought it would be something like "setSelection".

To be clear, I'm not trying to invoke the textbox selection - I'm talking about p / div / ul tags, etc.

I have linked to the following site (maybe this will give you an idea?):

http://www.quirksmode.org/dom/range_intro.html

Thanks in advance for your time.

+2


source to share


1 answer


Assuming you have a DOM range Range

in non-IE browsers and TextRange

IE:



function selectRange(range) {
    var sel;

    if (window.getSelection) {
        // Non-IE browsers
        sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (document.selection && range.select) {
        // IE
        range.select();
    }
}

      

+1


source







All Articles