Remove selection with javascript

I am working on a drag-and-drop image viewer. When the user drags the scroll bar, part of the text on the web page is selected. I tried

window.getSelection().removeAllRanges(); 

but this does not seem to work in IE7 / 8. Also tried

 document.getSelection().removeAllRanges(); 

which seems to work just as well as with IE7 / 8.

Are there any other ways to do this? I don't know any jQuery solutions, but if there is, let me know (:

EDIT: This is the onmouse-event context

 $("#slideBar").mousedown(function(f) { mouseDown = true; some more code... } $(document).mouseup(function() { if (mouseDown) { window.getSelection().removeAllRanges(); more code... } } 
+4
source share
2 answers

Try returning false to the functions you use, especially for the "mousedown" event. Also return false in document.onselectstart and document.ondragstart will help.

+6
source

You can add CSS (with jquery) to handle this:

 element { -moz-user-select: none; -webkit-user-select: none; user-select: none; } 

In any case, for IE you need to add your own element (with jquery) to this element:

 unselectable="on" 
+4
source

All Articles