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';
    li.appendChild(up);

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

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

ul.addEventListener('click', (event) => {
    if (event.target.tagName == 'BUTTON') {
        if (event.target.className=='remove') {
            let li = event.target.parentNode
            let ul = li.parentNode
            ul.removeChild(li)
        }
     }

    if (event.target.tagName == 'BUTTON') {
        if (event.target.className == 'up') {
            let li = event.target.parentNode;
            let ul = li.parentNode;
            let preLi = li.previousElementSibling;
            if (preLi ) {
                ul.insertBefore(li, preLi);
            }
        }
    }

    if (event.target.tagName == 'BUTTON') {
        if (event.target.className == 'down') {
            let li = event.target.parentNode;
            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');
    addButtons(li);
    li.textContent = newItem.value;
    ul.appendChild(li);
    newItem.value = '';

})
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <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>
        <ul>
          <li>grapes</li>
          <li>amethyst</li>
          <li>lavende</li>
          <li>plums</li>
        </ul>
        <input type="text" id="newItem">
        <button id="addNewElement">change list description</button><br /><br />
        <button id="removeItem">Remove item</button>
    </div>
    <script src="app.js"></script>
  </body>
</html>

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