Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                    <div id="container">
        <input type="text" id="new-todo" aria-label="Add a new item" placeholder="Add a new item..." />
        <ul id="todo" role="list"></ul>
        <ul id="done" role="list"></ul>
    </div>
    <div id="menu">
        <ul role="menu">
            <li><a role="menuitem" id="context__complete">I did it!</a></li>
            <li><a role="menuitem" id="context__uncomplete">I didn't do it</a></li>
            <li><a role="menuitem" id="context__delete">Delete it</a></li>
        </ul>
    </div>

              
            
!

CSS

              
                * {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    font-size: 14px;
}

body {
    background: #eee;
}

ul, li {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}

#container {
    width: 600px;
    margin: 10px auto;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 3px;
    min-height: 300px;
    box-shadow: 0px 0px 4px #888;
}

#new-todo {
    padding: 10px;
    border: none;
    width: 100%;
    box-sizing: border-box;
}

.todo-item {
    border-top: 1px solid #ddd;
    margin: 0px;
    padding: 10px;
    cursor: pointer;
}
.todo-item:hover {
    background: #0df;
}

.todo-item.completed,
.todo-item.completed:hover {
    background: #eee;
    color: #888;
    text-decoration: line-through;
}

#menu {
    box-sizing: border-box;
    width: 120px;
    height: auto;
    border: 1px solid #aaa;
    border-radius: 2px;
    padding: 3px;
    box-shadow: 0px 0px 3px #aaa;
    background-color: #fff;
    display: none;
    position: absolute;
    z-index: 10;
}

#menu ul li a {
    margin: 0px;
    padding: 3px 10px;
    border-radius: 5px;
    display: block;
}
#menu ul li a:hover {
    background-color: #ddd;
    cursor: pointer;
}

              
            
!

JS

              
                const container = document.querySelector('#container');
const input = document.querySelector('#new-todo');
const todoList = document.querySelector('#container ul#todo');
const doneList = document.querySelector('#container ul#done');
const menu = document.querySelector('#menu');
const menuList = document.querySelector('#menu ul');
const option__complete = document.querySelector('#menu #context__complete');
const option__uncomplete = document.querySelector('#menu #context__uncomplete');
const option__delete = document.querySelector('#menu #context__delete');
let myTodos = [];

const displayTodoList = json => {
    json.forEach((todo, i) => {
        displayTodo(todo);
    });
} 

const displayTodo = todo => {
    let todoText = document.createTextNode(todo.title);
    let todoEl = document.createElement('li');
    todoEl.id = `todo_${todo.id}`;
    todoEl.classList.add('todo-item');
    todoEl.setAttribute('role','listitem');
    if(todo.completed) {
        todoEl.classList.add('completed');
    }
    todoEl.appendChild(todoText);
    todoEl.addEventListener('click', handleClickTodo, true);
    todoEl.addEventListener('contextmenu', showContextMenu, true);
    if(todo.completed) {
        doneList.prepend(todoEl);
    }
    else {
        todoList.prepend(todoEl);
    }
}

const updateElement = todo => {
    let todoEl = document.querySelector(`#todo_${todo.id}`);

    if(todo.completed) {
        todoEl.classList.add('completed');
        doneList.prepend(todoEl);
    }
    else {
        todoEl.classList.remove('completed');
        todoList.prepend(todoEl);
    }
}

const removeElement = id => {
    let el = document.querySelector(`#todo_${id}`);

    el.remove();
}

const handleClickTodo = e => {
    let todo = {
        id: parseInt(e.target.id.replace('todo_','')),
        completed: !(e.target.classList.contains('completed'))
    }

    if(todo.id > 200) { // these don't actually exist on the server so we have to fake it
        updateElement(todo);
    }
    else {
        putTodo(todo);
    }
}

const showContextMenu = e => {
    e.preventDefault();

    let completed = e.target.classList.contains('completed');
    let itemObj = {
        id: parseInt(e.target.id.replace('todo_','')),
        completed: !completed
    }

    menuList.setAttribute('data-item',encodeURIComponent(JSON.stringify(itemObj)));

    if(completed) {
        option__complete.style.display = 'none';
        option__uncomplete.style.display = 'block';
    }
    else {
        option__complete.style.display = 'block';
        option__uncomplete.style.display = 'none';
    }

    menu.style.display = 'block';
    menu.style.left = e.pageX + "px";
    menu.style.top = e.pageY + "px";
}

const handleContextFunction = e => {
    let clicked = e.target.id;
    let itemObj = JSON.parse(decodeURIComponent(menuList.getAttribute('data-item')));

    switch(clicked) {
        case 'context__complete':
        case 'context__uncomplete':
            if(itemObj.id > 200) { // these don't actually exist on the server so we have to fake it
                updateElement(itemObj);
            }
            else {
                putTodo(itemObj);
            }
            menu.style.display = 'none';
            break;
        case 'context__delete':
            deleteTodo(itemObj.id);
            menu.style.display = 'none';
            break;
        default:
            break;
    }
}

const newId = () => (myTodos.reduce((big, t) => big = (big > t.id) ? big : t.id, 0)) + 1;

const buildNewTodo = (todo, id) => ({
    id: id,
    title: todo,
    completed: false
});

const getTodos = () => {
    try {
        myTodos = JSON.parse(localStorage.getItem("myTodos")) || [];
        displayTodoList(myTodos);
    } catch (err) {
        console.error(err);
    }
}

const addTodo = title => {
    try {
        let newTodo = buildNewTodo(title, newId());
        myTodos.push(newTodo);
        localStorage.setItem("myTodos", JSON.stringify(myTodos));
        displayTodo(newTodo);
    } catch (err) {
        console.error(err);
    }
}

const putTodo = todo => {
    try {
        let myUpdatedTodos = myTodos.map(t => {
            if(t.id === todo.id) {
                t.completed = todo.completed;
            }
            return t;
        });
        myTodos = myUpdatedTodos;
        localStorage.setItem("myTodos", JSON.stringify(myTodos));
        updateElement(todo);
    } catch (err) {
        console.error(err);
    }
}

const deleteTodo = id => {
    try {
        let myUpdatedTodos = myTodos.filter(t => t.id !== id);
        myTodos = myUpdatedTodos;
        localStorage.setItem("myTodos", JSON.stringify(myTodos));
        removeElement(id);
    } catch (err) {
        console.error(err);
    }
}

document.addEventListener('DOMContentLoaded', () => {
    input.addEventListener('keyup', e => {
        if(e.key === "Enter") {
            addTodo(input.value);
            input.value = '';
        }
    })
    
    window.addEventListener('keyup', e => {
        if(e.key === "Escape") {
            menu.style.display = 'none';
        }
    })

    menu.addEventListener('click', handleContextFunction);
    
    getTodos();
});

              
            
!
999px

Console