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

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

paraOne.firstElementChild;

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;

  newTask.appendChild(checkBox);
  newTask.appendChild(label);
  newTask.appendChild(inputText);
  newTask.appendChild(editButton);
  newTask.appendChild(deleteButton);




  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");
  incompleteTaskHolder.appendChild(moreTask);
  bindTaskEvents(moreTask, taskCompleted);
  //input (Checkbox)

    //label
    //input (texts)
    //button.edit
    //button.delete
    //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;
      incompleteTaskHolder.appendChild(newTask);
     bindTaskEvents(newTask,taskCompleted);
}



 //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
      completedTaskHolder.appendChild(newTask);
       bindTaskEvents(newTask,taskIncomplete);
    }

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++) {

  bindTaskEvents(incompleteTaskHolder.children[i],taskCompleted);

}
    //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
229,732 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.