473,804 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confused with hasChildNodes() method of DOM

9 New Member
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 3100
gits
5,390 Recognized Expert Moderator Expert
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
dasrasmikant
30 New Member
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="javas cript">
var a = document.getEle mentById('td2') ;
alert(a.hasChil dNodes());
</script>[/HTML]
Mar 27 '08 #3
gits
5,390 Recognized Expert Moderator Expert
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
connectwithme
9 New Member
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
connectwithme
9 New Member
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.tag Name " 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 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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.c om/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
1094
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 that were empty are NOT overwritten. So, the code is as follows: <code>
1
3274
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True Response.ContentType ="application/vnd.ms-excel" 'application/msword
3
1525
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
4947
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., <Root> <mysection>
0
1715
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., <Root> <mysection>
3
2171
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 for a member variable of a Python object. It has to be given a value when defined, or set within a method. But what is the difference between an Attribute of a Class, a Descriptor in a Class, and a Property in a Class?
2
1430
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 ‘Load(assmblyRef)’ method of AppDomain class, assembly is loaded but DLL file locked. I try to read DLL in byte array and load assembly calling Load(byte) method. All are pretty, but in contrast to calling Load(assmblyRef)’ method, calling...
1
3406
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
10410
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 used by other process. I thought about creating a method which will take care of the logging...all threads will used a delegate to call the method that does the logging static readonly object lockerLog = new object(); public delegate void...
0
9591
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
10594
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
10087
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
9166
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
7631
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
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
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
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.