tags:

views:

18

answers:

2

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?

A: 

How about using a JavaScript library like jQuery, which allows you to write CSS-selector-like-syntax insead of getElementById, and also has useful helper methods such as parent().

http://jquery.com/

TimS
The div and span don't have CSS. I cannot add them, since I am "Assuming I do not have control over anything except the contents of the script tags".
Brian
Sorry, to clarify, I should have said "jQuery allows you to write CSS-selector-like-syntax". see http://api.jquery.com/category/selectors/jQuery will do what you need, i.e. node.parent().children() ... see http://api.jquery.com/category/traversing/Another JS framework you could check out is http://www.prototypejs.org/
TimS
@TimS: This doesn't tell me how I am supposed to actually select the node. I do not see any selectors which let me select the script node itself.
Brian
How about looping though all of the foo elements using something like $('#foo').each() ? see http://api.jquery.com/each/
TimS
@Tims: How do I know which foo is the right one?
Brian
Sorry, I'm not sure what you mean by the 'right' one. You're looking to traverse the DOM, jQuery helps you do that and the docs tell you how... That's my answer. Exactly how you implement it is up to you :)
TimS
@TimS: No, I want the node where current script node is. This is a specific location. Suggesting I figure this out by reading up on JQuery is a non-starter. See my own answer for an example of a mechanism which determines where the currently-executing script node is using a horrifying hack.
Brian
I should also note that I cannot rely on the contents of <span id="bar">` being different, nor can I make the script have different inner code depending on location. I used different `<span>` tags to demonstrate the issue, but in reality the only goal is to find the currently executing script tag in the dom, without knowing any information about what is going on outside of the script.
Brian
A: 

Here is an incredibly horrifying way to solve this problem: Rather than trying to get the current node, create a new node with an arbitrary ID, then delete it when done.

<div id="foo">
<span id = "bar">456</span>
<script type="text/javascript">
    document.write('<span id = "bfrogtmp"></span>');
    var y = document.getElementById("bfrogtmp");
    document.write(y.parentNode.children['bar'].innerHTML);
    y.parentNode.removeChild(y);
</script>
</div>

<hr>

<div id="foo">
<span id = "bar">123</span>
<script type="text/javascript">
    document.write('<span id = "bfrogtmp"></span>');
    var y = document.getElementById("bfrogtmp");
    document.write(y.parentNode.children['bar'].innerHTML);
    y.parentNode.removeChild(y);
</script>
</div>
Brian