GetElementByID (). parentNode returns null

For some reason this happens very vaguely. His work is sometimes, and sometimes not. The same line of code for different "myid" under the same parent, the line is

document.getElementById("myid").parentNode returns null.

I am pretty sure that the "myid" element is not the root element, and its parent is the DIV that needs to be returned. I am using Firefox version 3.6.10.

Can anyone suggest any reason why this might happen?

EDIT: "myid" is a kind of text field or any other control. But parentnode is always DIV. Any controls that we add are always wrapped under the DIV. So basically, when something on the screen is updated, we get the parent node and replace innerhtml. innerhtml can be anything.

below: html i have -

 <div style="height: 334px; width: 769px; position: relative;"> <span style="display: inline-block; height: 13px; width: 61px; position: absolute; left: 393px; top: 84px;" bizappid="System856UserGroupAppPoint156d5elabel300" tabindex="-1" id="System856UserGroupAppPoint156d5elabel300">User Group</span> <input type="text" style="height: 20px; width: 221px; position: absolute; left: 503px; top: 77px;" bizappid="System856UserGroupAppPoint156d5etextBox190" class="formtextbox" tabindex="400" id="System856UserGroupAppPoint156d5etextBox190" readonly="readonly" name="System856UserGroupAppPoint156d5etextBox190"> </div> 

In this Html, suppose I get a ParentNode for a Span, but I don't get the same parentNode for an Input text element. Also, another weird thing: I just added a check message if getelementbyid is not null, then check its parentNode. It is then additionally added if parentNode is not null and then updates the operation. Now the control is not included in the parentNode is not a null condition.

+4
source share
2 answers

If the same "id" value is shared by two or more elements on your page, getElementById (may) returns a list of nodes instead of a DOM link. The node list instance does not have a parentNode property.

Do not use id values ​​for more than one element; this is the moral of this story.

+6
source

should it document.getElementById("myid").parentNode ?

+1
source

All Articles