By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: List of Items Task in HTML with DOM and JavaScript

+3 votes

Write a JS function that read the text inside an input field and appends the specified text to a list inside an HTML page.

There will be no input/output, your program should instead modify the DOM of the given HTML document.

Sample HTML:

<h1>List of Items</h1>
<ul id="items"><li>First</li><li>Second</li></ul>
<input type="text" id="newItemText" />
<input type="button" value="Add" onclick="addItem()">
  function addItem() {
    // TODO: add new item to the list


list of items javascript task

asked in JavaScript category by user Jolie Ann

2 Answers

+2 votes

Here is my solution:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
<h1>List of Items:</h1>
<ul id="items">
<input type="text" id="newItemText">
<input type="button" value="Add an Item" onclick="addItem()">

    function addItem() {
        let input = document.getElementById("newItemText");
        if (input.value.length === 0) {
        let text = input.value;
        let newLi = document.createElement("li");
        newLi.textContent = text;
        input.value = "";


answered by user matthew44
0 votes
function addItem() {
let text = document.getElementById("newItemText").value;
let li = document.createElement("li");
li.textContent = text;
let items = document.getElementById("items");
document.getElementById("newItemText").value = "";


answered by user mitko