Basics of web development

JavaScript DOM (Document Object Model)

Easy
20 min

JavaScript's DOM (Document Object Model) is an important concept in browser-based programming. It determines how the browser creates HTML page elements and their relationships.

The DOM is a browser-based programming API (Application Programming Interface) that provides a way to manipulate HTML page elements using JavaScript. It consists of a hierarchical tree structure, where each HTML element is a node and its child elements are nodes below it. The DOM provides a set of APIs that allow you to traverse the page's elements, modify their content, and add or remove elements.

The JavaScript DOM API provides several important features that allow you to manipulate elements of an HTML page. These include:

  1. getElementById: This property returns the element in the HTML page whose identifier matches the given ID.
  2. getElementsByTagName: This property returns all the elements of the HTML page that correspond to the given tag name.
  3. getElementsByClassName: This property returns all the elements on an HTML page that match the given class name.
  4. innerHTML: This property allows you to set the contents of an HTML element.
  5. parentNode: This property returns the parent node of the HTML element.
  6. childNodes: This property returns all child elements of the HTML element.
  7. createElement: This feature creates a new HTML element.
  8. appendChild: This property adds a new HTML element as a child element to an existing HTML element.

Using DOM in JavaScript can be very useful when you want to dynamically change page elements based on user action, or find an element on the page and read its value.

Practice

Kokeile itse!

Valitettavasti Replit-palvelu on muuttnut lennosta eikä enää anna suorittaa näitä koodeja suoraan selaimessa. Voit klikata alla olevaa "Open in Replit" linkkiä ja avata koodin Replit-palvelussa. Meillä on työn alla etsiä Replitille korvaaja.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.