Solved

IE Question: element.previousSibling returns phantom object

Posted on 2004-03-22
11
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

630 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