Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementByID().parentNode is returning null

Tags:

javascript

For some reason, this is happening very vaguely. Its working sometimes and sometimes it is not. The same line of code, for the different "myid" under same parent, the line -

document.getElementById("myid").parentNode is returning null.

I am quite sure that element "myid" is not a root element and its parent is a DIV which needs to be returned. I am using Firefox 3.6.10 version.

Can anyone suggest any reason why this could be happening?

EDIT: The "myid" here is some kind of a textbox or any other control element. But the parentnode is always DIV. Any controls we add are always wrapped under a DIV. So basically when something on the screen refreshes, we get the parent node and replace the innerhtml. The innerhtml could be anything.

given below is the 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, assume I am getting ParentNode for Span element, but I am not getting the same parentNode for the Input text element. Also one more strange thing is, I just added a check saying if getelementbyid is not null, then check its parentNode. Then further added if parentNode is not null then do refresh operation. Now the control is not coming inside the parentNode not null condition.

like image 411
Sachin Shanbhag Avatar asked Oct 15 '10 11:10

Sachin Shanbhag


1 Answers

If the same "id" value is shared by two or more elements on your page, then getElementById (may) return a node list instead of a DOM reference. A node list instance has no "parentNode" property.

Do not re-use "id" values for more than one element is the moral of this story.

like image 173
Pointy Avatar answered Sep 27 '22 17:09

Pointy