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


Courses Plus Student 13,306 Points

Help with a very simple to-do App with Javascript


following the front-end development track, I finished the course Web pages interactions. As I saw in the course, I wanted to build a to-do app by myself to check how I was going on my JS journey...

And it's pretty bad. I also tried to check the course solution but it didn't help.

So here is my pb: I have and add button which is working perfect and adding <li> element with buttons inside <ul> perfectly. So on the ongoing tasks, let's say i have 4 tasks, so it's 4 li's with 3 buttons (edit, approve, delete) for each li. When I click the delete button, i want the parent node li to be deleted, but, I can't figure out how to select the delete button dynamically depends on the delete button index i clicked on.

Hope I made myself clear, if not === I want the delete button to delete his parent li, how to do it.


<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="css/bootstrap-theme.min.css"> <link rel="stylesheet" href="css/bootstrap.min.css"> <link rel="stylesheet" href="css/main.css"> <link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'> <link href='https://fonts.googleapis.com/css?family=Martel:300' rel='stylesheet' type='text/css'> <title> To-do App Chrome Extension - Fully Javascript Loaded</title> </head> <body>

<div class="app">

<div class="row container-fluid addTodo">
    <h1>Add Tasks</h1>
    <input class="addTask" type="text">
    <button class="addTaskButton"><i class="glyphicon glyphicon-plus"></i></button>

<div class="separator row container-fluid"></div>

<div class="row container-fluid editTodo">
    <h1>Ongoing Tasks</h1>
    <ul class="ongoingList">
            Become a Javascript Master
            <button class="doneTaskButton"><i class="glyphicon glyphicon-ok-circle"></i></button>
            <button class="editTaskButton"><i class="glyphicon glyphicon-edit"></i></button>
            <button class="deleteOngoingTaskButton"><i class="glyphicon glyphicon-remove-circle"></i></button>

<div class="separator row container-fluid doneTodo">
    <h1>What you've done</h1>
    <span class="doneTask">Become a Javascript Master</span>
    <button class="deleteDoneTaskButton"><i class="glyphicon glyphicon-remove-circle"></i></button>


<script src="js/main.js"></script> </body> </html>


body { font-family: 'Martel', serif; }

h1, h2, h3, h4, h5, h6 { font-family: 'Pacifico', cursive; }

.app { width: 500px; margin: 0 auto; text-align: center; }

.glyphicon { font-size: 15px; top: 2px; padding: 5px; }

.doneTask { text-decoration: line-through; }

.addTodo { background-color: #6aabd0; width: 100%; margin: 0; padding-bottom: 20px; }

.editTodo { background-color: #d5777d; width: 100%; margin: 0; padding-bottom: 20px; }

.doneTodo { background-color: #6ac299; width: 100%; margin: 0; padding-bottom: 20px; }

.addTodo h1 { font-size: 30px; }


//1. Make button addTask work // 1.1 Add Task to ongoing task sections //define the needed elements

var addTaskButton = document.getElementsByClassName('addTaskButton')[0]; var deleteOngoingTask = document.getElementsByClassName('deleteOngoingTaskButton'); var deleteDoneTaskButton = document.getElementsByClassName('deleteDoneTaskButton'); var ongoingList = document.getElementsByClassName('ongoingList')[0];

var addTodoInput; var listItem; var doneButton; var editButton; var removeButton;

var addNewTodoTask = function() { addTodoInput = document.getElementsByClassName('addTask')[0].value; //Create List Element listItem = document.createElement('li'); //Create Buttons Elements for each task //Done Button doneButton = document.createElement('button'); doneButton.classList.add('doneTaskButton'); doneButton.innerHTML = '<i class="glyphicon glyphicon-ok-circle"></i>'; //Edit Button editButton = document.createElement('button'); editButton.classList.add('editTaskButton'); editButton.innerHTML = '<i class="glyphicon glyphicon-edit"></i>'; //Remove Button removeButton = document.createElement('button'); removeButton.classList.add('deleteOngoingTaskButton'); removeButton.innerHTML = '<i class="glyphicon glyphicon-remove-circle"></i>'; //Append Elements to li listItem.innerHTML = addTodoInput; listItem.appendChild(doneButton); listItem.appendChild(editButton); listItem.appendChild(removeButton); //Append li to ul ongoingList.appendChild(listItem); //.2 Empty the addTask input document.getElementsByClassName('addTask')[0].value = ""; };

addTaskButton.onclick = addNewTodoTask;

// //2. Enter Edit mode when clicking on EditButton // 2.1 Make the span input to change the text // 2.2 When click out, back as a span // //2.3 Remove button should remove ongoing task //2.4 Mark as done button should import tasks from ongoing to Done Tasks // // //3. Remove button from DoneTask should remove doneTasks

Thanks for your help, and BTW, the solution must be Javascript only, no jQuery library or kind of things.

(My design is bad, I know!)


Can you please re-format your JS and CSS, it is very difficult to read the JS. Oh and you want vanilla JS right?

1 Answer

take a look at the Markdown Cheatsheet above the post comment button.