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

Why it's not possible to just do this...

Hi, I can't understand this binding events function. Why can't we just instead ( ie. for fixing delete button ) do something simple like this:

// Select all buttons with delete class
var delbuttons = document.getElementsByClassName("delete");
// Function to delete "li" element
var deleteTask = function() {
  var listItem = this.parentNode;
  var ul = listItem.parentNode;
  ul.removeChild(listItem);
}
delbutton.addEventListener("click", deleteTask );

Shouldn't this now be ready on all delete buttons so once you click them you call deleteTask function ?

2 Answers

Hi Denis

I think the problem is that var delbuttons = document.getElementsByClassName("delete"); returns an array and it binds only the array and not the particular button to the click event.

Maybe you can try this and see if it works: for(var i = 0; i < delbutton.length; i++) { delbutton[i].addEventListener("click", deleteTask ); }

Good luck!

Yes, you're right. I read docs and thats the case. Thanks anyway.