473,770 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

childNodes returns 2 not 1 children

Good evening.

<span id=fileList>
<input type=hidden name=file id=file_0 value=Name/>
</span>
....
document.getEle mentById(fileLi st).childNodes

The childNodes is giving me two children: 1) file_0 and 2) undefined.

I can't understand why there is not just file_0 or what the undefined child
is.

Anyone have any ideas? (Ignore any syntax errors - I stripped the code from
a servlet.)

Thanks,
Ron

Jul 23 '05 #1
6 2307
Ron Brennan wrote:
Good evening.

<span id=fileList>
<input type=hidden name=file id=file_0 value=Name/>
</span>
...
document.getEle mentById(fileLi st).childNodes

The childNodes is giving me two children: 1) file_0 and 2) undefined.

I can't understand why there is not just file_0 or what the undefined child
is.

Anyone have any ideas? (Ignore any syntax errors - I stripped the code from
a servlet.)


Download Firefox (or Netscape or Mozilla) and use the DOM inspector.

In IE, a text node is inserted after the input element. In Firefox
(et al), text nodes are inserted after the span and the input and will
report ...childNodes.l ength = 3.

Try:

<input type="button" value="Show nodes..." onclick="
var x = document.getEle mentById('fileL ist').childNode s;
var t = [];
for ( var i=0; i<x.length; i++){
t[i] = 'Node ' + i + ': ' + x[i].nodeName + '\n';
}
alert(t.join('\ n'));
">
<span id="fileList">
<input type="hidden" name="file" id="file_0" value="Name" />
</span>


--
Rob
Jul 23 '05 #2

"RobG" <rg***@iinet.ne t.auau> wrote in message
news:aw******** **********@news .optus.net.au.. .
Ron Brennan wrote:
Good evening.

<span id=fileList>
<input type=hidden name=file id=file_0 value=Name/>
</span>
...
document.getEle mentById(fileLi st).childNodes

The childNodes is giving me two children: 1) file_0 and 2) undefined.

I can't understand why there is not just file_0 or what the undefined child is.

Anyone have any ideas? (Ignore any syntax errors - I stripped the code from a servlet.)


Download Firefox (or Netscape or Mozilla) and use the DOM inspector.

In IE, a text node is inserted after the input element. In Firefox
(et al), text nodes are inserted after the span and the input and will
report ...childNodes.l ength = 3.

Try:

<input type="button" value="Show nodes..." onclick="
var x = document.getEle mentById('fileL ist').childNode s;
var t = [];
for ( var i=0; i<x.length; i++){
t[i] = 'Node ' + i + ': ' + x[i].nodeName + '\n';
}
alert(t.join('\ n'));
">
<span id="fileList">
<input type="hidden" name="file" id="file_0" value="Name" />
</span>

--
Rob


Much appreciated Rob - thanks; My immediate problem turned out to be the
additional child was being created by a carriage return being generated by
the second println (I wrongly thought the servlet part to be irrelevant):

out.println("<t d></td><td id='fileList'>" );
out.println("<i nput type=\"hidden\" name=\"file\" id=\"file_0\"
value=\"Test Name\"/>");
out.println("</td>");

I've been working on this for days and found it within 5 minutes after
posting. Rob's information is something I didn't know and copied.
Jul 23 '05 #3
Ron Brennan wrote:
"RobG" <rg***@iinet.ne t.auau> wrote in message [...]
Much appreciated Rob - thanks; My immediate problem turned out to be the
additional child was being created by a carriage return being generated by
the second println (I wrongly thought the servlet part to be irrelevant):

out.println("<t d></td><td id='fileList'>" );
out.println("<i nput type=\"hidden\" name=\"file\" id=\"file_0\"
value=\"Test Name\"/>");
out.println("</td>");

I've been working on this for days and found it within 5 minutes after
posting. Rob's information is something I didn't know and copied.


The bottom line is that you need to be aware that different browsers
collapse whitespace differently. Any navigation you do up or down the
DOM tree should use a method that is independent of the number of
intervening nodes and not just assume that there are /x/ nodes between
here and there.

In the example you posted, getting to the first input from the span
could be:

// use getElementsByTa gName
function getFirstChildIn put( spanId ) {
if ( !docment.getEle mentById || !document.getEl ementsByTagName ) {
return null;
}
var x=document.getE lementById(span Id).getElements ByTagName('inpu t')
return ( x.length > 0 )? x[0] : null;
}
// walk down tree
function getFirstChildIn put( spanId ) {
if ( !docment.getEle mentById ){return null}
var x = document.getEle mentById(spanId ).childNodes;
var i = x.length;
while ( i-- ) {
if ( 'input' == x[i].nodeName.toLow erCase() ) {
return x[i];
}
}
return null; // or whatever if an input isn't found
}


--
Rob
Jul 23 '05 #4
RobG wrote:
[...]

Gahhh, always when you post...

// walk down tree
function getFirstChildIn put( spanId ) {
if ( !docment.getEle mentById ){return null}
var x = document.getEle mentById(spanId ).childNodes;
var i = x.length;
while ( i-- ) {
if ( 'input' == x[i].nodeName.toLow erCase() ) {
return x[i];
}
That will get the *last* input, the first is:

var i, j=x.length;
for ( i=0; i<j; i++ ) {
if ( 'input' == x[i].nodeName.toLow erCase() ) {
return x[i];
}
}
return null; // or whatever if an input isn't found
}

--
Rob
Jul 23 '05 #5

"RobG" <rg***@iinet.ne t.auau> wrote in message
news:p9******** **********@news .optus.net.au.. .
RobG wrote:
[...]

Gahhh, always when you post...

// walk down tree
function getFirstChildIn put( spanId ) {
if ( !docment.getEle mentById ){return null}
var x = document.getEle mentById(spanId ).childNodes;
var i = x.length;
while ( i-- ) {
if ( 'input' == x[i].nodeName.toLow erCase() ) {
return x[i];
}


That will get the *last* input, the first is:

var i, j=x.length;
for ( i=0; i<j; i++ ) {
if ( 'input' == x[i].nodeName.toLow erCase() ) {
return x[i];
}
}
return null; // or whatever if an input isn't found
}

--
Rob


Just checked in again, and picked up your advice and code. Many thanks
again, Ron

Jul 23 '05 #6
RobG wrote:
In IE, a text node is inserted after the input element. In Firefox
(et al), text nodes are inserted after the span [...]


Nothing is *inserted* here. The text nodes *are there* just because of
the formatted source code. It is but that Firefox behaves (according to
the standards) while IE does not, removing whitespace text nodes on some
not well-defined occasions from its DOM which is clearly a Bad Thing.
PointedEars
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5317
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a single, possibly compound, node. Why is a list of childNodes needed? Thx, Skip
7
3359
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in. since the taxonomy is a rather long list, i'm hiding and showing divs for the secondary and tertiary links, so when a user clicks on the checkbox for the parent category, the children appear in a second (and third) div, with checkboxes of their...
2
6247
by: Pierre | last post by:
Hi, was just wondering how I could set the ParentNode checked value to all its childNodes (and their childnodes... etc) Thks for help.
1
35898
by: craigkenisston | last post by:
Hi, I'm pretty newbie on XML so I have this basic question. I have a node which has 5 ChildNodes : <RelatedLink> <DataUrl type="canonical">clickhere2.com/</DataUrl> <NavigableUrl>http://clickhere2.com/</NavigableUrl> <Asin>B0000A1I5E</Asin> <Relevance>301</Relevance>
3
8495
by: Q1tum | last post by:
Hi all, I have a problem with getting the amount of childs in a XML structure, the strucure is somewhat like the following: <?xml version="1.0" encoding="iso-8859-1"?> <cms> <num>21</num> <xmlnames> <field>id</field>
3
3555
by: Jake Barnes | last post by:
This weekend I decided to play around with Javascript a little and try to teach myself some things about AJAX and DOM. I've been doing experiments on this page: http://www.publicdomainsoftware.org/ajaxExperiment.htm If you go there and click in any box, you'll get some controls. If you then click "Add an Image" you'll have the chance to add an image. But how many child elements are there in the box that communicates with you? I count...
1
8257
by: yawnmoth | last post by:
Given an element ID, is there a way to figure out what index one would need to use in the parentNode's childNodes array to get at that element? For example... <body> <div id="parent"> <div id="a">a</div> <div id="b">b</div>
1
1804
by: Sasi Kumar | last post by:
I have a xml file. I want to display the nodes and childnodes in my datagrid, i used xmldatasource, but i can read a specific path only. The problem is i should not use dataset and xmldocument. Give me some suggestions <?xml version="1.0" encoding="utf-8"?> <newbookingrs> <bookingid>187250</bookingid> <bookingstatus>We have received your request. As you did not submit the required payment details, no booking will be held for you. If...
4
3900
by: deepthisoft | last post by:
hi, Children element in javascript is not working in netscape. But is working fine in IE. ChildNodes element is also not working in Netscape. But it is working fine in IE. My code is look like this, function fSetSelectedDay(my) { alert(parseInt((my).childNodes("dateText").innerHTML)); alert((myElement.children.innerText));
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.