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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perfect

Arsalan Raja
Arsalan Raja
6,508 Points

document.getElementsByClassName();

why my code is not working when i try to get button value by getElementsByClassName method instead of querySelector method

var bindTaskEvents = function(taskListItem, checkBoxEventHandler)  {

console.log("Bind list item events");

  //select taskListItem's children
  var checkBox = taskListItem.querySelector("input[type=checkbox]");
  var editButton = taskListItem.querySelector("button.edit");
  var deleteButton = taskListItem.querySelector("button.delete");

  //bind editTask to edit button
  editButton.onclick = editTask;

  //bind deleteTask to delete button
  deleteButton.onclick = deleteTask;

  //bind checkBoxEventHandler to checkbox
  checkBox.onchange = checkBoxEventHandler;
}

Hi Arsalan,

Just to let you know, I edited your post just to format the code properly, if you need help doing this in future posts there is a handy Markdown Cheatsheet that will explain how to paste code into your forum posts.

As for your question, can you be more specific where you are encountering an issue?

1 Answer

getElementsByClassName requires you to run a loop to affect changes to all the elements you've selected, it doesn't work out of the box the way querySelector and querySelectorAll do.