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

jQuery Re-Write of the "To-Do List" Application

Hi everyone,

I'd like to share with you my jQuery re-write of the "To-Do List" application. You can interact with the project in full at Codepen, or you can take a look at the individual HTML, CSS and JavaScript/jQuery files over on Pastebin, but here's the JS:

'use strict';

var $taskInput = $("#new-task");
var $addButton = $("#add");
var $incompleteTasksList = $("#incomplete-tasks");
var $completedTasksList = $("#completed-tasks");

var taskItem = {

    create: function() {
        var name = $taskInput.val();

        if (name.length > 0) {
            var task = $('<li></li>');

            task.attr('name', 'Task');
            task.data('name', name);
            task.data('status', 'pending');

            task.append(addElement.checkbox('taskItem.toggleStatus(this)'));
            task.append(addElement.span(name));
            task.append(addElement.button('Edit', 'taskItem.edit(this)').addClass("edit"));
            task.append(addElement.button('Delete', 'taskItem.delete(this)').addClass("delete"));

            $incompleteTasksList.append(task);
            $taskInput.val('');

        }
    },

    toggleStatus: function(input) {
        var task = $(input).parents('li[name="Task"]');

        if (input.checked) {
            task.detach();
            $completedTasksList.append(task);
        } else {
            task.detach();
            $incompleteTasksList.append(task);
        }
    },

    edit: function(button) {
        var task = $(button).parents('li[name="Task"]');
        var span = task.children('span[name="Name"]');
        var edit = task.children('button[name="Edit"]');
        var save = addElement.button('Save', 'taskItem.save(this)');
        var input = $('<input>');

        input.val(task.data('name'));
        input.attr('name', 'Edit');

        span.replaceWith(input);
        edit.replaceWith(save);
    },

    save: function(button) {
        var task = $(button).parents('li[name="Task"]');
        var input = task.children('input[name="Edit"]');

        input.focus();
        task.data('name', input.val());

        var span = addElement.span(task.data('name'));
        var edit = addElement.button('Edit', 'taskItem.edit(this)');
        var save = task.children('button[name="Save"]');

        input.replaceWith(span);
        save.replaceWith(edit);

    },

        delete: function(button) {
        var task = $(button).parents('li[name="Task"]');

        task.remove();
    } 
};

// This object allows you to create consistent, functional HTML elements in the DOM without having to re-write code.

var addElement = {
    checkbox: function(onchange) {
        var checkbox = $('<input>');
        checkbox.attr('type', 'checkbox');
        checkbox.attr('onchange', onchange);

        return checkbox;
    },

    span: function(text) {
        var span = $('<span></span>');
        span.attr('name', 'Name');
        span.text(text);

        return span;
    },

    button: function(text, onclick) {
        var button = $('<button></button>');
        button.attr('onclick', onclick);
        button.attr('type', 'button');
        button.attr('name', text);
        button.text(text);

        return button;
    }
};

$taskInput.focus();
$addButton.on("click", taskItem.create);

Between full-time work and other commitments, this took me about a month, with a few stops and starts; I originally tried to closely follow Andrew Chalkley's pure JavaScript version, but ended up tying myself in knots with the editTask() function; I was thrilled to find help right here on the Treehouse forum, but I also spent quite a bit of time in the jQuery documentation and on Stack Overflow.

Eventually, a friend of mine showed me a much more object-oriented approach, which I was then able to "wire-up" to the slightly-edited HTML and CSS from the original, pure JavaScript version of the project. The projects works as intended, although if I were continuing to work on it, I'd consider making each task item draggable() within the task lists, among other things.

I'd love to hear your feedback or suggestions. I'm unlikely to continue working on this specifically, but I'm going to continue in my JavaScript learning journey and I look forward to seeing how I could do better.

Cheers!

1 Answer

Steven Parker
Steven Parker
243,318 Points

Very nice looking code — great job! :+1: I would've loved to see draggable items, but I certainly understand you wanting to move on after a month!

I'm happy to learn that my previous question response was helpful to you, and also happy to see that people actually do scan the archives now and then. :wink:

Happy coding!   -sp:sparkles:

Cheers Steven!