Selection

Selection 2

The "Selection" object identifies the highlighted selection to be worked on.
A selection is created by dragging the mouse over a section of text.

The "createRange()" method is used to convert a "selection" object to a "textRange" object:

document.selection.createRange()

In Mozilla the window.getSelection() method is used.

Highlight some text then click Show Selection

Highlight some text then click Remove Selection

Highlight some text then click Unselect Selection


Entering from a selection by using the default feature of double or triple clicking, or by selecting


The following is IE only

Highlight some text then click Colour Selection

Highlight some text then click bold Selection

Highlight some text then click Add To Selection

Highlight some text then click Replace Selection



This example shows one way of copying text into a textarea box within a popup window.

A modeless window is best used for this as you can still access the window below the popup whilst keeping the popup in view.

Open the Copy Window, highlight any text on this page then click this link or double click in the popup window.
The text you highlighted is pasted into the textarea box within the popup.
You can still edit the contents of the popup window.

var oRange = oTextbox.createTextRange()
oRange.moveStart("character", 0)
oRange.moveEnd("character", 3 - oTextbox.value.length)
oRange.select()
oTextbox.focus()

Moz

javascript:d=window.getSelection()
alert(d.toString().length)

javascript:d=window.getSelection()
alert(d.getRangeAt(0))

avascript:d=window.getSelection()
alert(d.isCollapsed)

if (window.getSelection) { txt = window.getSelection(); } else if (document.getSelection) { txt = document.getSelection(); } else if (document.selection) { txt = document.selection.createRange().text; }