I am experimenting with a Javascript function which stuffs nodes into a position. Something like appendChild(document.getElementById("foo")) . However, I do not have full control over the surrounding code and it is likely that some elements will have identical IDs. Consider the following:
<div id="foo">
<span id = "bar">456</span>
<script type="text/javascript">document.write(document.getElementById("bar").innerText);</script>
</div>
<hr>
<div id="foo">
<span id = "bar">123</span>
<script type="text/javascript">document.write(document.getElementById("bar").innerText);</script>
</div>
In most browsers, the output will be:
456 456
123 456
rather than what I want, which is
456 456
123 123
This result is totally unsurprising to me; I do understand why this is happening. I was hoping I could do a javascript call more like
document.write(myscriptnode.parent.childnodes['bar'].innerText)
Assuming I do not have control over anything except the contents of the script tags, is there a way to accomplish this?