Element.length

  • Revision slug: DOM/element.length
  • Revision title: element.length
  • Revision id: 194780
  • Created:
  • Creator: Jabez
  • Is current revision? No
  • Comment /* Example */

Revision Content

{{template.DomRef()}}

Summary

length returns the number of items in a list.

Syntax

no_of_items = nodeList.length

Parameters

  • no_of_items is an integer value representing the number of items in a list.

Example

// all the paragraphs in the document 
items = document.getElementsByTagName("p"); 
// are there any at all? 
if ( items.length ) {
     // for each item in the list,
     // append the entire element as a string of HTML
     for (var i = 0; i < items.length; i++) {
         gross += items[i].innerHTML;
         // gross is now all the HTML for the paragraphs
     } }

Notes

length is a very common property in DOM programming. It's very common to test the length of a list (to see if it exists at all) and to use it as the iterator in a for loop, as in the example above.

Specification

length

{{ wiki.languages( { "pl": "pl/DOM/element.length" } ) }}

Revision Source

<p>
{{template.DomRef()}}
</p>
<h4 name="Summary">Summary</h4>
<p><b>length</b> returns the number of items in a list. 
</p>
<h4 name="Syntax">Syntax </h4>
<pre class="eval"><i>no_of_items</i> = nodeList.length
</pre>
<h4 name="Parameters">Parameters </h4>
<ul><li> <code>no_of_items</code> is an integer value representing the number of items in a list.
</li></ul>
<h4 name="Example">Example </h4>
<pre>// all the paragraphs in the document 
items = document.getElementsByTagName("p"); 
// are there any at all? 
if ( items.length ) {
     // for each item in the list,
     // append the entire element as a string of HTML
     for (var i = 0; i &lt; items.length; i++) {
         gross += items[i].innerHTML;
         // gross is now all the HTML for the paragraphs
     } }
</pre>
<h4 name="Notes">Notes </h4>
<p><b>length</b> is a very common property in DOM programming. It's very common to test the length of a list (to see if it exists at all) and to use it as the iterator in a for loop, as in the example above. 
</p>
<h4 name="Specification">Specification </h4>
<p><a class="external" href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-203510337">length </a>
</p>{{ wiki.languages( { "pl": "pl/DOM/element.length" } ) }}
Revert to this revision