Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Navagating DOM Element properties in console.

So I enjoy the lessons but I'm trying to figure if I understand things or I'm just following along. I created my own variable

var paraOne = document.getElementsByTagName("p");

It returns p in the console. I want to select the first child but


returns undefined. It should be the label element. Can you help explain what I'm donig wrong?

// Problem: User interaction doesn't provide desired results.
// Solution: Add interactivity so the user can manage daily tasks.

let taskInput = document.getElementById('new-task');//new-task
let addButton = document.getElementsByTagName('button')[0]; //first button
let incompleteTaskHolder = document.getElementById('incomplete-tasks');
let completedTaskHolder = document.getElementById('completed-tasks');
let paraOne = document.getElementsByTagName("p");

//Add a new task

let createNewTaskElement = function(){

let newTask = document.createElement("li");  
let checkBox = document.createElement("input");
let label = document.createElement("label");
let inputText = document.createElement("input");
let editButton = document.createElement("button");
let deleteButton = document.createElement("button");

  //When the button is pressed
  //Create a new list item with the text from #new-task:

  label.innerText = taskstring;


  return newTask;


  let reLabel = function (){  
    let bob = paraOne.querySelector("input[type=text]");
    let paraVal = paraOne.getAttribute("");

    return reLabel;

let addTask = function() {
  let moreTask = createNewTaskElement("some new string");
  bindTaskEvents(moreTask, taskCompleted);
  //input (Checkbox)

    //input (texts)
    //Each elements, need modified and appended


 //Edit an existing task
let editTask = function() {
    //When the Edit button is pressed
      //if the class of the parent is .editMode
        //Switch from .editMode
        //label texst become the input's value


  //Delete an existing task
    let deleteTask = function() {
      console.log("box is checked");
       //When the Delete button is pressed
          //Remove the parent list item from the url


   let taskIncomplete = function() {
   console.log("Task incomplete..");
     let newTask = this.parentNode;

 //Mark a task as complete
    let taskCompleted = function() {
     //When the checkbox is unchecked
      //Append the task list item to the #incomplete-task
      let newTask = this.parentNode

let bindTaskEvents = function(taskListItem, checkBoxEventHandler){
  console.log("bind list item events");
//Select taskListItem's children
    let checkBox = taskListItem.querySelector("input[type=checkbox]");
    let editButton = taskListItem.querySelector("button.edit");
    let 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;

//Set the click handler to teh addTask funciton
addButton.onclick = addTask;

//Cycle over the incompleteTaskHolder ul list item
for(let i = 0; i < incompleteTaskHolder.children.length; i++) {


    //bind events to the list items children (taskCompleted)

for(let i = 0; i < completedTaskHolder.children.length; i++) {
  bindTaskEvents(completedTaskHolder.children[i], taskIncomplete);


2 Answers

Steven Parker
Steven Parker
216,012 Points

The property firstElementChild applies to individual elements.

But getElementsByTagName returns an element collection. You can get a single element from a collection by indexing, if you know which item you need.

For example, if paraOne is intended to be the first (index 0) paragraph, you could write this:

var paraOne = document.getElementsByTagName("p")[0];

Thank you, that was driving me crazy.