/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] dom question

hi all!!! I have a question relative to DOM in this code:

[code]
<html>
<head>
<script type=”text/javascript”>

function myFunction()
{

alert(“Length of the list=”+document.getElementById(“users_list”).childNodes.length);
};

</script>
</head>
<body>
<dl id=”users_list”>

<dt onclick=”myFunction()”>
This is the first Item
</dt>
</dl>

</body>
</html>
[/code]

The output of this code yields “Length of the list=3”, that is, the number of children of the element dl is three but he number of items in the list is only one!! Why??

Thank you very much!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 17.2012 — there are 2 textnodes and one dt node, which contains one text node.

try it like this:
[CODE]<body>
<dl id="users_list"><dt onclick="myFunction()">
This is the first Item
</dt></dl>
</body>[/CODE]


The new lines between the tags in your code are childnodes, just as they would be if you had any character text there.
Copy linkTweet thisAlerts:
@hebrerilloauthorMar 17.2012 — Thank you very much mrhoo!!! now it works!!!
×

Success!

Help @hebrerillo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...