views:

33

answers:

1

Hi there
I do not whether I should ask be asking this question. I need to getElementbyid for an object which is already exisiting in the document
. In the example, I would like to get the element "test" which is sub of parentDiv1. Could anyone help? It is ok with ff but not with IE. Any tips?

Example:

<div id="parentDiv1">
    <ul id="test1">test</ul>
</div>
<div id="parentDiv2">
    <ul id="test2">test</ul>
</div>
<script>
    var prtDiv1 = document.getElementById("parentDiv1");
    var test1 = prtDiv1.getElementById("test1");
</script>
+1  A: 

You shouldn't have more than one element with the same id in your document. Use a class. The easiest way for you to do what you want then would be to use jQuery and write $('#parentDiv1 ul.test') to select your element. Other than jQuery, you would need to implement getElementsByClassName in javascript in IE because afaik it still doesn't support it natively.

Edit: Make sure there aren't any "name" attributes that have the same value as the target id, and also that the js variable you're setting has a different name than the target id.

jeremiahd
OP's script referred to `#test1`, so it isn't a duplicate ID problem, rather a typo. I've fixed that.
Delan Azabani
Try setting the name of the variable to something other than the dom id. I seem to remember IE having some sort of problem with that.
jeremiahd
thank u. js wat i need.
tweakmy