JavaScript

DOM

DOM#


What you will learn

What is DOM

DOM (Document Object Model) is created by the browser. It is a tree-like structure made up of all the elements that exist in your HTML document.

#

DOM (Document Object Model) provides a structured representation of the document (a tree) and it defines a way that the structure can be accessed from programs so that they can change the document structure, style and content. DOM is made up of all the elements that exist in your HTML document.

There are many JavaScript built-in functions, which are used to access and to manipulate DOM elements. See Manipulating documents. DOM manipulation was easier with jQuery and it is covered in jQuery lecture in this course. However jQuery popularity is going down and today it is easy to select and manipulate DOM with vanilla JavaScript. In addition by using jQuery library we have worse performance. A good resources of Vanilla JavaScript DOM functions (some with reference to jQuery) are:

Accessing DOM Elements

document object gives an access to the DOM. Type document in the console to see all its fields and methods. DOM access functions by JavaScript are:

Note: all the above functions (beside getElementById) can be used on the selected element. For example:

var ul = document.querySelector( 'ul' );
var li = ul.querySelectorAll( 'li' ); 
// or by chaining
li = document.querySelector( 'ul' ).querySelectorAll( 'li' );

The functions getElementById, getElementsByTagName and getElementsByClassName are very specific and much faster, while querySelector and querySelectorAll are more elaborate.

The getElement methods return live DOM elements while the querySelector method does not. querySelector returns elements which are known at the time of execution of this search (will not recognize elements that are added later). Live DOM elements update as you change them by adding, removing and changing values.

Notes:

Summary from Searching Elements:

#

Examples

Example - Get element by ID:

var element = document.getElementById( 'my-id' );
// or
var element = document.querySelector( '#my-id' );

Example - Get elements by class name:

var elements = document.getElementsByClassName( 'my-class' );
// or
var elements = document.querySelectorAll( '.my-class' );
for ( var i = 0; i < elements.length; i++ ) {
    var element = elements[ i ];
    element.style.color = "red";
}

Example - Get elements by tag name:

var elements = document.getElementsByTagName( 'li' );
var elements =  document.querySelectorAll( 'nav ul li' );

Example - use querySelectorAll:

var images = document.querySelectorAll( 'img' );
for ( var i = 0; i < images.length; i++ ) {
    var image = images[ i ];
    alert( image.getAttribute( src ) );
}

Note: querySelector and querySelectorAll take the full range of CSS selector syntax variations as its argument.

Example - use data attribute:

<div data-expand>
Some content that can be collapsed or expanded.
</div>

var expand = document.querySelectorAll( '[data-expand]' );

Example - use data attribute:

<div data-action="expand">
Some content that can be collapsed or expanded.
</div>

var expand = document.querySelectorAll( '[data-action="expand"]' );

Example:

Define a function like jQuery to get an element:

var $ = function ( selector, parent ) {
    return ( parent ? parent : document ).querySelector( selector ); 
};
var $$ = function ( selector, parent ) {
    return Array.prototype.slice.call( ( parent ? parent : document ).querySelectorAll( selector ) ); 
    // ES6
    // return [ ...(parent ? parent : document ).querySelectorAll( selector ) ]; 
};
// call example
var el = $( '.my-class' );
var elArr = $$( '.my-class' );