Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

olu adesina
olu adesina
23,007 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 (event.target.tagName == 'BUTTON') {
        if (event.target.className=='remove') {
            let li = event.target.parentNode
            let ul = li.parentNode

    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');
    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
229,004 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