Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSelection() broken in IE10

I use the window.getSelection() method on my project to make a quotable text

It's working just fine in all modern browsers, exept IE10. In IE10 console returns correct text, but the selection is broken.

The only code I used:

text = window.getSelection().toString();
console.log(text);

This code calls on mouseup event.

Does anyone know the solution?

like image 271
Bohdan Khodakivskyi Avatar asked Apr 27 '13 21:04

Bohdan Khodakivskyi


People also ask

Does getselection work in IE9?

@Alex IE prior to 9 does not implement getSelection (). The code works for me in Chrome 8. I selected the text and clicked the button, and the text became red, so something works. @Alex Your demo works in all current browsers ( IE9, FF4, Chrome 10, Safari 5, and Opera 11). +1 This is a great getSelection demo.

What does the getselection () function return?

The getSelection () property of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret. None. A Selection object. Some functions (like Window.alert ()) call toString () automatically and the returned value is passed to the function.

What is the use of getselection in document interface?

Document.getSelection () The getSelection () property of the Document interface returns a Selection object representing the range of text selected by the user, or the current position of the caret.

How do I get the selection range of an input element?

You can call Window.getSelection (), which works identically to Document.getSelection () . It is worth noting that currently getSelection () doesn't work on the content of <input> elements in Firefox. HTMLInputElement.setSelectionRange ()) could be used to work around this.


1 Answers

try this should work for ie < 9.0

var texttest = document.selection.createRange();
alert (texttest.text);

this is for all browsers except ie < 9.0

var texttest = document.getSelection();
alert(texttest);
like image 142
Hichem Avatar answered Oct 16 '22 04:10

Hichem