I am new to javascript, trying to perform:
document.selection.createRange();
but document.selection
always returns undefined
.
I am using latest version of chrome.
what am I doing wrong?
thanks!
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.
getSelection() The Window. getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.
The basic concept of selection is Range, that is essentially a pair of “boundary points”: range start and range end. A Range object is created without parameters: let range = new Range(); Then we can set the selection boundaries using range.
Use window.getSelection()
, which is the most cross-browser compatible (it's supported in the current versions of all major browsers) and is the standard. Chrome certainly supports it as fully as other browsers.
document.selection
should only be used for IE < 9.
Try document.getSelection()
or window.getSelection()
.
Here's a quick example that I tested in chrome
http://jsfiddle.net/hgDwx/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With