The Array .push method is not working for me!

I don’t know what is going on! Here’s my program:

var my_List = []

And that array goes into this funtion:

function addToTheList(thingToAdd) {
  my_List.push(thingToAdd)
  var newListItem = document.createElement("li")
  newListItem.innerHTML = myList[myList.length - 1]
  myListArea.appendChild(newListItem)
}

This is what shows up in the console

I don’t know what’s going on!
Full programming is at jsfiddle.net/CatsLover2006/j10f1n5y/25/

What does the line that calls addToTheList() look like; you’ve not listed that here.

Hi @chancelorne! In the fiddle, you have this line:

addButton.addEventListener("click", addTheThing())

So you’re not binding addTheThing as a listener function, you’re immediately calling it and passing its return value (which is undefined); and when you are doing this, my_List has not been initialised yet. Also note the typo – sometimes you’re calling it my_List, sometimes myList.

Thanks! Now it works (but I have to figure out why it says[object HTMLInputElement] in the line when I put in ‘A Cat’. Well, thanks anyways!

because you print/stringify the input element instead of its value.

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