You are here: Reference > JavaScript > client-side > selection and ranges > objects > controlRange

controlRange collection

Browser support:
Represents a collection of controls. A control can be a button, select, textarea, one of the input elements or an arbitrary element in contentEditable mode.
There are two ways to get a controlRange collection: The controlRange collection provides methods for accessing and manipulating its contents. For details, see the pages for the select and execCommand methods.

Syntax:

Methods that return the object:
body.createControlRange ( )
selection.createRange ( )
selection.createRangeCollection ( )

Possible members:

Properties:
length
Returns an integer that specifies the number of objects in the current collection.

This property is read-only.
Methods:
add (control)
Inserts a control into the current collection at the last position.
This method is identical to the addElement method.

Parameters:

control
Required. Reference to the control to add.

Return value:

This method has no return value.
addElement
Inserts a control into the controlRange collection at the last position.
execCommand
Allows running commands on certain objects.
item (index)
Returns an object from the controlRange collection.

Parameters:

index
Required. Zero-based integer that specifies the object to retrieve.

Return value:

The element at the specified position, or null
queryCommandEnabled
Returns whether the execution of the specified command can be successful, using the execCommand method.
queryCommandIndeterm
Returns whether the specified command is in an indeterminate state.
queryCommandState
Returns the current state of the specified command.
queryCommandSupported
Returns whether the specified command is supported by the current object.
queryCommandText
Returns the description of the specified command.
queryCommandValue
Returns the actual value of the specified command.
remove (index)
Removes a control from the current collection by index.

Parameters:

index
Required. Zero-based integer that specifies the position of the element to remove.

Return value:

This method has no return value.
scrollIntoView
Scrolls the specified element into the visible area of the document.
select
Selects all text or controls that is included in the current TextRange or controlRange object.

Example HTML code 1:

This example illustrates the use of the controlRange collection via the createControlRange method:
<head>
    <script type="text/javascript">
        function Init () {
                // the 'multipleSelection' command throws an exception in Opera
            try {
                    // enables multiple selection in Internet Explorer
                document.execCommand("multipleSelection", false, true);
            }
            catch (e) {};
        }

        function SelectButtons () {
            var editor = document.getElementById ("editor");
            if (editor.createControlRange) {        //Internet Explorer
                var buttons = editor.getElementsByTagName ("button");

                var controlRange = editor.createControlRange ();
                for (var i = 0; i < buttons.length; i++) {
                    controlRange.add (buttons[i]);
                }

                controlRange.select ();
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body onload="Init ();">
    <div id="editor" contenteditable="true" style="background-color:#e0f0e0;">
        <button>Sample button</button>
        <br /><br />Sample text inside the editor.<br /><br />
        <button>Another button</button>
    </div>
    <br /><br />Click on the button below to select all buttons with the controlRange collection.<br /><br />
    <button onclick="SelectButtons ();">Select all buttons inside the editor!</button>
</body>
Did you find this example helpful? yes no

Example HTML code 2:

This example creates a controlRange collection from the current selection:
<head>
    <script type="text/javascript">
        function Init () {
                // the 'multipleSelection' command throws an exception in Opera
            try {
                    // enables multiple selection in Internet Explorer
                document.execCommand("multipleSelection", false, true);
            }
            catch (e) {};
        }

        function TestSelection () {         
            if (document.selection === undefined || document.selection.type === undefined) {
                alert ("Your browser does not support this example!");
                return;
            }

            if (document.selection.type == "Control") {
                var controlRange = document.selection.createRange ();
                alert ("There are " + controlRange.length + " buttons selected.");

                for (var i = 0; i < controlRange.length; i++) {
                    var button = controlRange.item (i);
                    alert ("The label of the " + (i+1) + ". selected button: " + button.value);
                }
            }
            else {
                alert ("Please select some of the buttons or maybe the selection cannot contain controls in your browser!");
            }
        }
    </script>
</head>
<body onload="Init ();">
    Select some of the buttons in the field below and click on the 'Test Selection' button.<br />
    You can select a button by clicking on it.<br />
    Hold down the CTRL or SHIFT key for multiple selection.
    <br /><br />
    <div contenteditable="true" style="background-color:#a0e0e0;">
        <button>First button</button><br />
        <button>Second button</button><br />
        <button>Third button</button>
    </div>
    <br /><br />
    <button onclick="TestSelection ();">Test Selection</button>
</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content