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
- HTML5 A vocabulary and associated APIs for HTML and XHTML, Section 7.6.1
Attributions
Mozilla Developer Network : [Selection.addRange Article]
Microsoft Developer Network: [addRange Method Article]