473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

confused with hasChildNodes() method of DOM

I am using IE 6.x & Mozilla 2.x
This is the HTML code

[HTML] <td id="td2">
<tr id="td3">
<td id="td4"> Some Test TEXT </td>
<td id="td5"> <input type=checkbox value="chk-4">CheckBox 4 </td>
</tr>
</td>[/HTML]

this is the Javascript Code

Expand|Select|Wrap|Line Numbers
  1. var a = document.getElementById('td2');
  2.  alert(a.hasChildNodes());
the "hasChildNodes()" should give the result as TRUE but it is giving FALSE

WHY ?????????
Mar 27 '08 #1
7 3087
gits
5,390 Expert Mod 4TB
its a corrupt markup and so the dom might be corrupt ... you cannot have a <tr> in a <td> ...

try to fix this and see whether the problem persists ...

kind regards
Mar 27 '08 #2
I am using IE 6.x & Mozilla 2.x
This is the HTML code

[HTML] <td id="td2">
<tr id="td3">
<td id="td4"> Some Test TEXT </td>
<td id="td5"> <input type=checkbox value="chk-4">CheckBox 4 </td>
</tr>
</td>[/HTML]

this is the Javascript Code

Expand|Select|Wrap|Line Numbers
  1. var a = document.getElementById('td2');
  2.  alert(a.hasChildNodes());
the "hasChildNodes()" should give the result as TRUE but it is giving FALSE

WHY ?????????

It can be true in this way

[HTML]<table id="td2">
<tr>
<td ><table>
<tr id="td3">
<td id="td4"> Some Test TEXT </td>
<td id="td5"> <input type=checkbox value="chk-4">CheckBox 4 </td>
</tr>
</table>
</td>
</tr>
</table>
<script language="javascript">
var a = document.getElementById('td2');
alert(a.hasChildNodes());
</script>[/HTML]
Mar 27 '08 #3
gits
5,390 Expert Mod 4TB
as you see with valid markup it will work ;) using dom methods requires much more care with the markup that forms the dom-tree ...

kind regards
Mar 27 '08 #4
Thanks, Yes you are right, Now I have modified the HTML to
[HTML]<td>
<table> <tr> <td></td> </tr> </table>
</td>[/HTML]

I works for now.. let me work on my logic I will get back on the result
Mar 27 '08 #5
Looks like the DOM APIs are very strict about the way the document is structured

When I use this structure.

[HTML]<td >
<table id="TAB2">
<tr id="td3">
<td id="td4"> Some Test TEXT </td>
<td id="td5"> <input type=checkbox value="chk-4">CheckBox 4 </td>
</tr>
</table>
</td>[/HTML]

when I try this
Expand|Select|Wrap|Line Numbers
  1.  var c =  document.getElementById("tab2");
  2.   var d = c.childNode.id 
I am expecting TD3 but when I check the " c.childNode.tagName " it return "TBODY".

If this is how the DOM API works then I think I will just have abandon the code
using DOM API do something different.

PLEASE HELP ME IF I AM CORRECT
Mar 27 '08 #6
gits
5,390 Expert Mod 4TB
tbody is the correct output correct ... have a look at the w3c html spec ... using childNodes, nextSibling etc. may be confusing ... especially with tables and whitespaces in your html-document ... but you just have to be aware of it

kind regards
Mar 27 '08 #7
pronerd
392 Expert 256MB
Looks like the DOM APIs are very strict about the way the document is structured
DOM is an XML standard. For it to work you have to follow correct XML syntax rules, which you really want to try to do anyway. The browser wars of the late 90s allowed us to write a lot of really bad code that can not be reliably parsed as you are seeing. Following the correct syntax rules is a big time saver in the long run, and is starting to become a requirement as we switch to XHTML.

If you are not aware of XML syntax the W3CSchool site has a lot of helpful material on XML and XHTML. If you are not using an editor that can validate your HTML/XHTML it would be a big help to switch to one.

http://www.w3schools.com/xhtml/default.asp



If this is how the DOM API works then I think I will just have abandon the code
using DOM API do something different.

PLEASE HELP ME IF I AM CORRECT
There are a number of syntax issues in the examples. First off id attributes should be descriptive and unique to avoid confusion with actual tag names and or reserver words. Secondly since the tag id is 'TAB2' so you need to call 'TAB2' instead of 'tab2'.
Mar 27 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: chris yoker via DotNetMonster.com | last post by:
hiya, I previously obtained code that enabled me the replace a node and its innerText.The code relied on the node that was to be replaced having ?childNodes? However, it seems that the nodes...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
3
by: Brian | last post by:
? nChild.OuterXml "<LocaleId>{76135713-CB5C-4116-925F-1E69254A775B}</LocaleId>" ? nChild.HasChildNodes True TEXT IS NOT A NODE!!! Please explain.
1
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg.,...
0
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg.,...
3
by: redefined.horizons | last post by:
I've been reading about Python Classes, and I'm a little confused about how Python stores the state of an object. I was hoping for some help. I realize that you can't create an empty place holder...
2
by: =?Utf-8?B?UGFi?= | last post by:
Hello, I am confused with load assembly in AppDomain. I created ASP Web application, same business logic is realized in custom DLLs, and I need to load ones in my application. If I use...
1
by: =?Utf-8?B?QmFydCBTdGV1cg==?= | last post by:
I have the following issue. I'm trying to read to following XML doc. <Main> <Submain> <SubField1/> <SubField2/> </Submain> <Field1/> <Field2>AnyTextValue</Field2> </Main>
9
by: inxanu | last post by:
Hi all, I have several threads running (basically they are copying files from one place to another). I am using log4net for logging...However, sometimes i get a log4net error due to log file being...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.