JavaScript JavaScript and the DOM Traversing the DOM Getting All Children of a Node with children

olu adesina
olu adesina
22,696 Points

my function is not working (JavaScript)

the function is based in an click event handler which creates a new li. the addButtons function is meant to add buttons to each li created but its not

const toggle = document.querySelector('#toggleList');
const list = document.querySelector('#list');
const input = document.querySelector('input');
const p = document.querySelector('p.description');
const button = document.querySelector('button.description');
const newItem = document.querySelector('#newItem');
const addNewElement = document.querySelector('#addNewElement');
const removeItem = document.querySelector('#removeItem');
const listItems = document.getElementsByTagName('li');
const ul = list.getElementsByTagName('ul')[0];

function addButtons(li) {

    let up = document.createElement('button');
    up.className = 'up';
    up.textContent = 'up';

    let down = document.createElement('button');
    down.className = 'down';
    down.textContent = 'down';

    let remove = document.createElement('button');
    remove.className = 'remove';
    up.textContent = 'remove';

ul.addEventListener('click', (event) => {
    if ( == 'BUTTON') {
        if ('remove') {
            let li =
            let ul = li.parentNode

    if ( == 'BUTTON') {
        if ( == 'up') {
            let li =;
            let ul = li.parentNode;
            let preLi = li.previousElementSibling;
            if (preLi ) {
                ul.insertBefore(li, preLi);

    if ( == 'BUTTON') {
        if ( == 'down') {
            let li =;
            let ul = li.parentNode;
            let nextLi = li.nextElementSibling;
            if (nextLi) {
                ul.insertBefore(nextLi, li);



button.addEventListener('click', () => {
p.textContent= input.value

p.title = "list Discription";

addNewElement.addEventListener('click', () => {
    let ul = document.querySelector('ul')[0];
    let li = document.createElement('li');
    li.textContent = newItem.value;
    newItem.value = '';

<!DOCTYPE html>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p class="testing">Making a web page interactive</p> 
    <button id="toggleList"> hide list</button>
    <div style="background:pink" id="list">
        <p class="description" >Things that are purple:</p>
        <input type="text" class="description">
        <button class="description">change list description</button>
        <input type="text" id="newItem">
        <button id="addNewElement">change list description</button><br /><br />
        <button id="removeItem">Remove item</button>
    <script src="app.js"></script>

1 Answer

Steven Parker
Steven Parker
207,994 Points

The addButton function itself works (though it puts the wrong label on one of the buttons). But it is not called for the existing items to add buttons to them.

Then, when a new item is added, there are two issues in the click handler for the addNewElement button:

  • an index of 0 is applied to the result of querySelector which causes "ul" to be undefined
  • li.texContent is changed after the buttons are added which then removes them