mozilla

Revision 400567 of Selection.addRange()

  • Revision slug: Web/API/Selection/addRange
  • Revision title: addRange
  • Revision id: 400567
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment FixDOM/Selection/addRange Web/API/Selection/addRange

Revision Content

{{DomRef}}

Summary

Adds a {{domxref("Range")}} to a {{domxref("Selection")}}.

Syntax

sel.addRange(range)

Parameters

range
A {{ domxref("Range") }} object that will be added to the {{ domxref("Selection") }}.

Examples

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

var strongs = document.getElementsByTagName("strong");
var s = window.getSelection();

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

for(var i = 0; i < strongs.length; i++) {
  var range = document.createRange();
  range.selectNode(strongs[i]);
  s.addRange(range);
}

Revision Source

<div>{{DomRef}}</div>


<h2 id="Summary" name="Summary">Summary</h2>

<p>Adds a {{domxref("Range")}} to a {{domxref("Selection")}}.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox"><em>sel</em>.addRange(<em>range</em>)</pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<dl>
  <dt><em><code>range</code></em></dt>
  <dd>A {{ domxref("Range") }} object that will be added to the {{ domxref("Selection") }}.</dd>
</dl>

<h2 id="Examples" name="Examples">Examples</h2>

<pre class="brush:js">
/* Select all STRONG elements in an HTML document */

var strongs = document.getElementsByTagName("strong");
var s = window.getSelection();

if(s.rangeCount &gt; 0) s.removeAllRanges();

for(var i = 0; i &lt; strongs.length; i++) {
  var range = document.createRange();
  range.selectNode(strongs[i]);
  s.addRange(range);
}</pre>
Revert to this revision