Appending issues?

I’m trying to append when I click something, but instead the value is undefined and it’s no longer a Li that I’m appending.

Any help?

The return value of the .append method is undefined (that’s just written into JavaScript):

            /*  <-     this part is undefined    ->   */

list.append(document.createElement('li').append('Hello'))


// results in:

list.append(undefined)

To avoid appending undefined, you can split it up, for example like this:

  const li = document.createElement('li');
  li.append('Hello') // this would also work: li.textContent = 'Hello'
  list.append(li);
1 Like

So basically don’t combine append() into one another right?

Basically, yes. But you could use a different method, this for example would work:

list.appendChild(document.createElement('li')).append('Hello')

Reason being that .appendChild returns the element it has appended. You can’t use it to append a text string, only DOM elements. Once that method has returned the li element, you can chain .append to add the text.

1 Like

Thank you my good man, appreciate it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.