The DOM (concept)

Computer Science · Web development

Question 1 of 10

0

    What is the nodeName of a p element in the DOM?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    The DOM (concept) explained

    The Document Object Model, or DOM, is a tree that a browser builds from an HTML page. Each element and each piece of text in the page becomes a node that programs can read and change.

    When a browser loads an HTML document, it parses the markup and builds the DOM, a tree with the document at the top and nested elements below. Parent nodes contain child nodes, and siblings share the same parent. Text inside an element is stored in its own text node.

    Programs written in JavaScript use the DOM to find nodes, read their content and change them. Changing a node updates what is shown on screen, which is how interactive pages work without reloading. Methods that select nodes by id, class or tag find the nodes to work on, and methods such as appending a child add new ones.

    Worked example

    Consider this small HTML fragment, with the whitespace between tags left out of the count.

    <ul>
      <li>Apples</li>
      <li>Pears</li>
    </ul>
    
    1. The root of the fragment is the ul element.
    2. It has two li element children.
    3. Each li has one text node child, containing "Apples" and "Pears".

    Total nodes: 3 element nodes plus 2 text nodes, which is 3+2=53 + 2 = 5. In a real browser the line breaks and indentation between the tags also become whitespace text nodes, so the count is higher.

    What you will practise

    104 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • The DOM represents:
    • Scripts can:
    • An element node:
    • Parent-child relationships:
    • DOM manipulation updates:
    • The top-level element of an HTML document is:
    • Which method returns the first element that matches a CSS selector?
    • Text written inside an element, such as the words in a paragraph, is stored in a:

    …and 96 more.