Get DOM element given its string index in the document's HTML
<p>I have my document's HTML source as a string and the index <code>i</code> at which an element begins in this string. </p>

<p>I would like a function <code>getElementByIndex(i)</code> that returns the JavaScript DOM object corresponding to the element beginning at position <code>i</code> in the page's HTML string.</p>

<p>For example, I would expect the following behavior:</p>

<pre><code>&gt; var markup = document.documentElement.innerHTML;
&gt; markup

    "&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;div id="a"&gt;&lt;/div&gt;&lt;div id="b"&gt;&lt;/div&gt;&lt;/body&gt;"

&gt; getElementByIndex(19)        // 19 is the index of the first div's 
                               // '&lt;' character in the markup string

     &lt;div id="a"&gt;&lt;/div&gt;

&gt; getElementByIndex(19) === document.getElementById("a")

     true

&gt; getElementByIndex(19) === document.getElementById("b")

     false
</code></pre>