Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

IE Question: element.previousSibling returns phantom object

Posted on 2004-03-22
11
Medium Priority
?
309 Views
Last Modified: 2010-05-18

<html>
<body>

<form>
<input type='text'>
<span onclick='alert(this.previousSibling.tagName)'>click me</span>
</form>

</body>
</html>

When you click on "click me", the result should be a popup box that says, "INPUT".  However, the result is "undefined".

If you change the reference from "this.previousSibling.tagName" to "this.previousSibling.previousSibling.tagName" it works.  Seems like there's some phantom object between the "INPUT" tag and the "SPAN" tag.  NOTE:  I added some code to display the number of children below the "FORM" tag.  The result was 2.  Weird.

However, if I replace the "INPUT" tag with some non-form tag, I only need one "previousSibling" reference to get the object...which makes sense.

Does anybody know if this is a known bug or if there is some extra object that pads form tags that I don't know about?  I'd like to know before I start widely using the "Sibling" references with form tags.  BTW, the same phenomenon applies for "nextSibling".

0
Comment
Question by:2perplexed
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 total points
ID: 10653773
Depends on the browser.  Some will treat the line feed as a node, so previousSibling in that case has no tag.   That is actually the correct implementation of the standard. try putting the span on the same lins as the input like this:

<input type='text'><span onclick='alert(this.previousSibling.tagName)'>click me</span>

It has messed my up a few times when I try to process the childnodes in a loop nad get the white space text nodes.  The way to avoid problems is to check the node types which are text for the phantom whitespace nodes.

Cd&
0
 
LVL 31

Expert Comment

by:seanpowell
ID: 10653813
That's incredible. I had subscribed to the Q for interest sake - knowing that you would know.

Interesting how white space/line feeds cause so many problems all around.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10654054
Now it just clicked in on the CSS Q with the row colors as well.  Funny how things run in cycles sometimes.

Cd&
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 31

Expert Comment

by:seanpowell
ID: 10654155
0
 
LVL 27

Expert Comment

by:Asta Cu
ID: 10654292
The "white space" issue is problematic here as well.  http://www.experts-exchange.com/Web/Browser_Issues/Q_20925992.html
Listening to learn.
Asta
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10654296
A different face of the same thing.

Cd&
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10654310
lol... looks like an invasion of white space problems this week.

Cd&
0
 
LVL 1

Author Comment

by:2perplexed
ID: 10657056
If you say that white space, or line feeds, etc. show up as extra child nodes, then why, when I add code to prompt me as to the number of child nodes, do I get "2" as the child node count?  I should get "3", right?
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10658821
actually when I name the form "theform", theform.childNodes.length returns 4 which is correct because there is also whispace after the span

Cd&
0
 
LVL 1

Author Comment

by:2perplexed
ID: 10658928
COBOLdinosaur,

That worked great when I got rid of the white space.  Only one more question.  Is there a good way to test to see if the node in question is in fact a white space node?  I tried the .type attribute you mentioned in your first answer but only got that to work on the INPUT tag, eg. <INPUT type='text> gave me "text" as the type.  When I tried it on the white space node, I got "undefined".

Thanks...
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10659848
The actual name of the property is nodeType
The types are:


ELEMENT_NODE                   = 1;
ATTRIBUTE_NODE                 = 2;
TEXT_NODE                      = 3;
CDATA_SECTION_NODE             = 4;
ENTITY_REFERENCE_NODE          = 5;
ENTITY_NODE                    = 6;
PROCESSING_INSTRUCTION_NODE    = 7;
COMMENT_NODE                   = 8;
DOCUMENT_NODE                  = 9;
DOCUMENT_TYPE_NODE             = 10;
DOCUMENT_FRAGMENT_NODE         = 11;
NOTATION_NODE                  = 12;
 
The only ones you really have to worry about are 1 an 3
3 can be white space, or actual text.  1 is a tag.

The nodes look like this:

http://www.mozilla.org/quality/browser/standards/dom1/tcmatrix/node.html

Thanks for the A. :^)

Cd&
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question