Question : Please explain me this javascript example about the DOM

Hey experts,

There are some concepts of js that I dont understand. First of all, I don't understand why the length of items is 15. Second, why if a do: alert(purchases.length); I get I message saying undefinded? isn't suppose to be an object? thanks and I appreciate your explanation.

<body id="www-XXX-com">

<h1> What to buy</h1>
<p title="a gentle reminder">Dont forget got buy this stuff</p>
<ul id="purchases">
      <li>A tin of beans</li>
<script type="text/javascript">
      var shooping = document.getElementById("purchases");
       var items = document.getElementsByTagName("*");


Answer : Please explain me this javascript example about the DOM

"items.length" of items is really the count of how many items are in "items".
Since you have asked for byTagName("*"), every single tag in the document is counted, to end up with 15 items, including <ul>, 3x<li> etc

>> alert(purchases.length);

purchases is never defined in your javascript.


is a better bet since you have defined it as getElementById('purchases')
It is a single element, so it has no ".length" property.
Random Solutions  
programming4us programming4us