tsunami
log in
email
password
links
newest items
tag list
syntax reference
tag:time
history
pointed to by
Javascript SetSelection
add
item name
tags
==Links - "QuirksMode reference"[1] - "Cross-browser selection reference"[2] [1]http://www.quirksmode.org/dom/w3c_range.html [2]http://www.webreference.com/js/column12/crossbrowser.html ==Introduction Javascript can be used to programmatically detect and alter text selection, both of regular rendered text and textareas. =Firefox Firefox's text selection abilities can be discovered by starting at the `"Range"[1]` object, which is usually obtained via `window."getSelection"[2]()."getRangeAt"[3](0)`. Apparently there can be multiple ranges, but I don't see when this would happen. It would probably be good to assert that `rangeCount == 1`. The plaintext version of the currently selected text can be called via `window.getSelection().getRangeAt(0).cloneContents().textContent`. [1]http://developer.mozilla.org/en/docs/DOM:range [2]http://developer.mozilla.org/en/docs/DOM:window.getSelection [3]http://developer.mozilla.org/en/docs/DOM:Selection:getRangeAt =Internet Explorer IE uses the `"TextRange"[1]` object. One obtains it via calling `"createTextRange"[2]()` on body, textarea, or input. [1]http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp [2]http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createtextrange.asp ==Disabling text selection Assign the following function to be the "event handler"[1] of an element's `"onmousedown"[2]` event. This is useful for drag and drop, as is demonstrated in my "drag and drop tutorial"[3]. [1]http://developer.mozilla.org/en/docs/DOM:element#Event_Handlers [2]http://developer.mozilla.org/en/docs/DOM:element.onmousedown [3]http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx {{ function OnMouseDown(e) { // prevent text selection in IE document.onselectstart = function () { return false; }; // prevent text selection (except IE) return false; } }}
some permissive license goes here
contact