- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - DOM Collections
The DOM (Document Object Model) collections are a way to group together related HTML elements. They are read-only and can be accessed using the properties of DOM objects such as the document object or a DOM node.
There are many different types of DOM collections, including:
The HTMLCollection object is an array-like list (collection) of HTML elements.
The NodeList object is a list (collection) of nodes extracted from a document.
The Form element collection in HTML DOM is used to set or return the collection of all <input> elements inside a form element.
The HTML DOM forms collection is used for returning all the form elements that are present inside the HTML document as a collection.
DOM collections can be used to perform a variety of tasks, such as:
Traversing the DOM
Adding, removing, or modifying elements
Changing the style or content of elements
Responding to user events
This tutorial provides basic understanding of DOM collections, particularly HTMLCollection object. The other type of DOM collections are discussed in next chapters.
The HTMLCollection Object
An HTMLCollection object is an array-like data structure of HTML elements. When you use the getElementByTagName()method to access DOM elements, it returns an HTMLCollection object.
It is the same as an array but not an array. You can traverse the HTML collection and access each HTML element through the index, but you can use the pop(), push(), etc. methods with HTML collection.
The methods and properties given below return the HTML collection.
getElementByTagName() method
getElementByClassname() method
children property
Properties and Methods of HTMLCollection Object
Here, we have listed the properties and methods that can be used with HTML collections.
Method / Property | Description |
---|---|
length | To get a count of HTML elements in the collection. |
item() | To get elements from the specific index. |
namedItem() | To get the HTML element using its id from the given collection. |
Example: Traversing the Collection Elements
In the below code, we have added the list of numbers.
In JavaScript, we access all <li> elements using the getElementByTagName() method, which returns the HTML collection.
After that, we use the for...of loop to traverse each HTML element. The collection contains each HTML element in the object format. We used the innnerHTML property with each element of the collection to get its HTML and print it on the web page.
<DOCTYPE html> <html> <body> <ul> <li> One </li> <li> Two </li> <li> Three </li> </ul> <div id = "output"> </div> <script> const output = document.getElementById('output'); let lists = document.getElementsByTagName('li'); for (let list of lists) { output.innerHTML += "inner HTML - " + list.innerHTML + "<br>"; } </script> </body> </html>
Example: Getting the length of the collection
In the below code, we used the 'length' property of the collection to get the number of elements in the collection.
<DOCTYPE html> <html> <body> <div class = "text"> JavaScript </div> <div class = "text"> HTML </div> <div class = "text"> CSS </div> <div class = "text"> CPP </div> <div id = "output">The length of the collection is: </div> <script> const divs = document.getElementsByClassName('text'); document.getElementById('output').innerHTML += " " + divs.length; </script> </body> </html>
Example: Using the namedItem method with collection
In the below code, we access all <div> elements using the getElementByClassName() method, returning the collection of <div> elements.
After that, we used the namedItem() method to access the <div> element having id equal to 'JavaScript'.
<DOCTYPE html> <html> <body> <div class = "text" id = "JavaScript"> JavaScript </div> <div class = "text" id = "PHP"> PHP </div> <div class = "text" id = "Python"> Python </div> <div class = "text" id = "Java"> Java </div> <div id = "output">The Element having id equal to JavaScript is: </div> <script> const output = document.getElementById('output'); const langs = document.getElementsByClassName('text'); output.innerHTML += langs.namedItem('JavaScript').innerHTML; </script> </body> </html>
Collections of document Object and DOM Elements
The document object contains some built-in collection to get the elements from the document.
In the below table, we have listed all collections which can be accessed using the document object.
Collection Name | Description |
---|---|
document.links | To get all <a> elements from the document. |
document.forms | To get all <form> elements from the document. |
document.images | To get all <img> elements from the document. |
document.scripts | To get all <script> elements from the document. |
document.styleSheets | To get all the document's <link> and <style> elements. |
Element.children | To get a collection of all children of the particular HTML element. |
element.attributes | To get the collection of all attributes of the given element. |
element.options | To get all <options> elements from the document. |
element.classList | To get a collection of class names of a particular DOM element. |