School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,468 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,720 people online right now. Registration is fast and FREE... Join Now!




undefined child element

 

undefined child element

rackham

30 Jun, 2009 - 01:15 AM
Post #1

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 3


My Contributions
Hello everyone, I have a problem with my javascript, im new to it so please bear with me.

I have a function which looks like

CODE

function mainLeftElements(){
    var containerDiv = document.getElementById('mainLeft');
    var elems = containerDiv.childNodes;
    alert(elems.length);
}


which for a div declared like so

CODE

<div id = 'mainLeft'></div>


alerts me it has a length of 1, trying to work out what the element is (thinking it might be a blank text element or something, maybe the id attribute also??) by using

CODE

elems[0].tagName


and

CODE

elems[0].id


i get confused as they both return 'undefined', so what is it?

Thanks for any help you can offer me,

Rackham

User is offlineProfile CardPM
+Quote Post


SoLi

RE: Undefined Child Element

30 Jun, 2009 - 03:20 AM
Post #2

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
For me your code alerts a length of 0. Are you sure the code you have posted on this thread is exactly the same as in your original source?

Indeed had you had a space character (or even line breaks) inside your div, the length would correctly be 1, as a child TEXT_NODE would exist.

Furthermore TEXT_NODE's (not being Element nodes) correctly do not have an ID or tagName attribute set, which is why those would return undefined.

If you are sure there are no text characters inside your div, please post which browser you are using to test this script.

Thanks.

This post has been edited by SoLi: 30 Jun, 2009 - 03:23 AM
User is offlineProfile CardPM
+Quote Post

SoLi

RE: Undefined Child Element

30 Jun, 2009 - 03:26 AM
Post #3

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
Here you can see a list of methods supported by text nodes (there is only one):

http://www.w3.org/TR/REC-DOM-Level-1/level...l#ID-1312295772

compared with element nodes:

http://www.w3.org/TR/REC-DOM-Level-1/level...ml#ID-745549614
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:38AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month