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.

JavaScript

OZIO FRANCE
PLUS
OZIO FRANCE
Courses Plus Student 13,306 Points

Help with dummy to-do app in JS

Hello,

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 element with buttons inside 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.

HTML :

<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>





<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>

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

<div class="row container-fluid editTodo">
    <h1>Ongoing Tasks</h1>
    <ul class="ongoingList">
        <li>
            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>
        </li>
    </ul>
</div>

<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>
</div>

CSS:

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; }

JS:

//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 = ''; //Edit Button editButton = document.createElement('button'); editButton.classList.add('editTaskButton'); editButton.innerHTML = ''; //Remove Button removeButton = document.createElement('button'); removeButton.classList.add('deleteOngoingTaskButton'); removeButton.innerHTML = ''; //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!)

Thanks

Casey Ydenberg
Casey Ydenberg
15,622 Points

Hi Simon,

It would be easier to help you if you put your code into a CodePen (http://codepen.io/) and formatted with line breaks/indentations etc..

Casey