Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear a selection in Firefox

I have this function

function smth() {
var container = null;
var newContainer = null;
if (window.getSelection) {  // all browsers, except IE before version 9
    alert("first if");
    var selectionRange = window.getSelection();
    if (selectionRange.rangeCount > 0) {
        var range = selectionRange.getRangeAt(0);
        container = range.commonAncestorContainer;
        newContainer = container;
    }
}
else {
    if (document.selection) {   // Internet Explorer
        alert("second if");
        var textRange = document.selection.createRange();
        container = textRange.parentElement();
    }
}

if (newContainer) {
    return newContainer.nodeName;
}
else {
    alert("Container object for the selection is not available!");
}
}     

Now after i do what i need to do with the selection i need to clear it. i tried a few things nothing worked, any ideas?

document.selection.clear ()    

this didnt work.

like image 835
Ovi Avatar asked May 31 '11 11:05

Ovi


People also ask

How do I clear selectively history in Firefox?

to open the menu panel. Select how much history you want to clear: Click the drop-down menu next to Time range to clear to choose how much of your history Firefox will clear (the last hour, the last two hours, the last four hours, the current day or everything).


1 Answers

For the problematic browser:

document.selection.empty()

For other browsers:

window.getSelection().removeAllRanges()

See http://help.dottoro.com/ljigixkc.php

like image 117
August Karlstrom Avatar answered Oct 18 '22 14:10

August Karlstrom