This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

addRange

Summary

Adds a Range to the current selection.

Method of dom/Selectiondom/Selection

Syntax

var result = selObj.addRange(/* see parameter list */);

Parameters

range

Data-type
Range

Range to add.

Return Value

Returns an object of type NumberNumber

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Examples

/* Select all STRONG elements in an HTML document */

var strongs = document.getElementsByTagName('strong');
var selObj = window.getSelection();

if(selObj.rangeCount > 0) selObj.removeAllRanges();

for(var i = 0; i < strongs.length; i++) {
  var range = document.createRange();
  range.selectNode(strongs[i]);
  selObj.addRange(range);
}
function selectElements(tagName){
var els = document.getElementsByTagName(tagName);
var selObj = window.getSelection();
if(selObj.rangeCount > 0) selObj.removeAllRanges();
        for(var i = 0; i < els.length; i++) {
          var range = document.createRange();
          range.selectNode(els[i]);
          selObj.addRange(range);
        }
   }

Notes

Remarks

Windows Internet Explorer 9 and higher and Webkit browser do not currently support multiple or disjointed selections in standards mode. If addRange is applied to a selection that already contains a Range, the new Range is not added.

Syntax

selObj.addRange(range)

Standards information

Attributions