Exercise 10.10

This is Exercise 10.10, lecture on modifying DOM with Javascript, used in the course IDATA2301 Web technologies at NTNU, campus Aalesund.

Go back to exercise list.

Purpose

The purpose of the exercise is to learn how you can remove elements from the DOM and add new ones.

Instructions

  1. Create a document with a button, title of the button: "Add".
  2. Create Javascript which adds a <p>Hello!</p> to the document every time the user presses the button.
  3. Modify the code: the "Add" button must add text Hello #!, where X is incremented each time: Hello 1!, Hello 2!, Hello 3!, etc.
  4. Create another button with text "Remove".
  5. Write Javascript code for handling clicks on the "Remove" button - when the user clicks it, delete the first text paragraph on the page.
  6. Disable the "Remove" button when there are no more paragraphs left, enable it again when at least one paragraph is present.

Solution

You can find a solution here.