getElementbyId problem in Firefox

T

TR

My document had the following nested DIV structure, with a possible
<SPAN> between the DIV and the <A> elements:

<div id="container">
<div id="D1">
<span>blah blah</span><br>
<A id="A1">item</a><br>
 
D

DU

TR said:
My document had the following nested DIV structure, with a possible
<SPAN> between the DIV and the <A> elements:

<div id="container">
<div id="D1">
<span>blah blah</span><br>
<A id="A1">item</a><br>
.
.
.
<A id="A99" onmousedown='AOCP(1,99)'>item</a><br>
<div id="D2">
<A id="A76" onmousedown='AOCP(1,65)'>item</a><br>
.
.
.
<A id="A89" onmousedown='AOCP(1,34)'>item</a><br>
</div>
</div>

I see 3 div start tag and 2 div end tag.
When the user puts the mouse down on any ANCHOR, my script needs to
determine the anchor's parent DIV. The following script worked at first

85 function getParentDiv(elementid) {
86 /* elementid is the id of the <A> where the user is clicking;
the <a> may be nested inside a <SPAN> so loop until first DIV is
encountered; */
87
88 var activeElement = document.getElementById(elementid);
89 do
90 {
91 if (activeElement.tagName != 'DIV')
92 activeElement = activeElement.parentElement;
93 }
94 while ( (activeElement.tagName != 'DIV') || (activeElement.tagName
=='HTML') )
95
96 if ((activeElement != null) && (activeElement.tagName == 'DIV') )
97 return activeElement.id;
98 else
99 return -1;
100
101
102 } /* end function */


But the script stopped working with this error "Line 94 activeElement
has no properties" (taken from the Firefox javascript console) when I
merely added a second container DIV ("outercontainer"):


<div id="outercontainer">
<div id="container">
<div id="D1">
<span>blah blah</span><br>
<A id="A1">item</a><br>
.
.
.
<A id="A99" onmousedown='AOCP(1,99)'>item</a><br>
<div id="D2">
<A id="A76" onmousedown='AOCP(1,65)'>item</a><br>
.
.
.
<A id="A89" onmousedown='AOCP(1,34)'>item</a><br>
</div>
</div>
</div>

4 start tag, 3 end tag.
I don't understand why this error is occuring when my former structure
has merely been enclosed within a DIV. Could someone please explain?
After a certain depth of nesting, does getElementById stop working in
Firefox? The script continues to work fine in IE6 and Opera 7.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040206
Firefox/0.8
Thanks
TR

1- Is your document perfectly valid? No improper nesting error reported
by the W3C HTML validator?
http://validator.w3.org/

2- Can you post an url? It is not only more convenient, making your post
smaller but it will show the http headers which is sometimes decisive
when trying to solve a problem

DU
 
R

Richard Cornford

TR wrote:
88 var activeElement = document.getElementById(elementid);
89 do
90 {
91 if (activeElement.tagName != 'DIV')
92 activeElement = activeElement.parentElement;
^^^^^^^^^^^^^
parentElement - is a Microsoft proprietary property that has been
implemented in some other browsers but not in Mozilla/Gecko browsers.
The W3C Core DOM property with the most similar characteristics is -
parentNode -.

<snip
I don't understand why this error is occuring when my former structure
has merely been enclosed within a DIV. Could someone please explain?
After a certain depth of nesting, does getElementById stop working in
Firefox? The script continues to work fine in IE6 and Opera 7.

As parentElement was always undefined in Firefox it is unlikely that
this code ever did what you thought it was doing in that browser.

Richard.
 
J

Joe B

Thank you for the information. I was using devguru.com as my
online reference and would be grateful if you could suggest
another site that is comparable in approach but which identifies
the proprietary things. Do you know of a good one?
TR
P.S. Opera appears to support activeElement if Firefox does not.
 
J

Joe B

Thanks for the reply. The missing closing tags were a feature of
my question, not of the HTML in the actual page. The HTML there is
valid. We're still working on the site and don't have it loaded
onto a webserver at the moment, or I'd be happy to provide a URL.
It looks as though the problem arose from my inadvertent use of a
Microsoft proprietary property.
TR
 
R

Richard Cornford

Joe said:
Thank you for the information. I was using devguru.com as my
online reference and would be grateful if you could suggest
another site that is comparable in approach but which identifies
the proprietary things. Do you know of a good one?

I cannot recommend any specific online references, I take my own
approach towards finding out what features browsers support:-

P.S. Opera appears to support activeElement if Firefox does not.

If you mean - document.activeElemnet - then yes, Opera introduced it
around version 7.20, but I don't see the relevance to your original
post. If you actually meant - parentElement - then Opera has supported
that since version 5, at least while spoofing IE (and as a normal part
of the DOM in versions 7.0+), it is also available in Safari/Konqueror
and IceBrowser (at least), but while Gecko browser don't implement it
the W3C DOM - parentNode - property should be preferred (and maybe -
parentElement - used as a fall-back for pre-W3C DOM browsers resembling
IE 4).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top