Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Dawning+of+a+New+Day|Euphoria+Script" rel="stylesheet">
  <title>A handy little to-do list!</title>
</head>
<body>
  <div id="to-do-list">

    <header>
      <h1>A handy little to-do list!</h1>
      <p>What do you need to do today?</p>
      <p>List your jobs and tasks right here. And then do them!</p>
    </header>

    <section>
      <form action="#" id="input-form">
        <input type = "text" id = "task-input" value = "New task" onfocus="this.value=''" onblur = "'New task'"><br>
        <input type = "submit" value="Add task" id = "add-task-button" >
      </form>
    </section> <!-- End of the input form section -->

    <section id = "task-list-main">
      <h2>Tasks</h2>
      <ul id = "task-list"></ul>
    </section>
  </div>

  <script src="javascript.js"></script>
</body>
</html>
            
          
!
            
              * {
  color: #1f3446;
  padding: 0;
  font-family: arial, helvetica, sans-serif;
}

html {
  background-image: url(https://s33.postimg.cc/sxeqofhcv/background.png);
}

body {
  display: flex;
  margin-top: 2rem;
}

#to-do-list {
  margin: auto;
  padding: 2rem;
  width: 80%;
  max-width: 700px;
  border: 2px dotted #365978;
  border-radius: 10%;
  background-color: rgba(233,123,80,0.6);
}

h1 {
  color: #365978;
  text-align: center;
  font-family: 'Euphoria Script', cursive;
  font-size: 3.5rem;
}

h2 {
  color: #365978;
  text-align: center;
  font-family: 'Euphoria Script', cursive;
  font-size: 2rem;
  margin-top: 7px;
}

p {
  text-align: center;
}

/* ---------------------------- */
/* ----- Add item section ----- */
/* ---------------------------- */

#input-form {
  text-align: center;
}

#task-input {
  padding: 7px 15px;
  margin-bottom: 15px;
  background-color:rgba(0, 0, 0, 0);
  border: 1px dotted #365978;
  width: 300px;
  max-width: 75%;
}

#add-task-button {
  padding: 7px 15px;
  margin: 5px;
  background: #365978;
  border: solid 1px #406b91;
  color: #ced0d4;
  border-radius: 5px;
  font-size: 1.05rem;
}

  #add-task-button:hover {
    background: #ced0d4;
    color: #365978;
  }

  #add-task-button:active {
    background: #d8dade;
    border-color: #d8dade;
  }

/* ----------------------------- */
/* ----- Main list section ----- */
/* ----------------------------- */

#task-list-main {
  border: 2px dotted #365978;
  border-radius: 5%;
  margin-top: 25px;
}

li {
  list-style-type: none;
  padding-top: 15px;
}

.item-delete {
  position: inline-block;
  float: right;
  margin-left: 20px;
}

    .item-delete:hover {
      opacity: 0.6;
    }

.task-list-item {
  border-top: 2px dotted #365978;
  padding: 12px;
  padding-left: 30px;
}
            
          
!
            
              // Declare variables for user interface elements

const taskInput = document.querySelector('#task-input');
const addTaskButton = document.querySelector('#add-task-button');
const taskList = document.querySelector('#task-list');
const inputForm = document.querySelector('#input-form');

// Event listeners

callEventListeners();

function callEventListeners() {
  // DOM Load event
  document.addEventListener('DOMContentLoaded', getTasks);
  // Add task event
  inputForm.addEventListener('submit', addTask);
  // Delete task event
  taskList.addEventListener('click', removeTask);
}

// Retrieve existing tasks from local storage
function getTasks() {
    let tasks;
    if(localStorage.getItem('tasks') === null){
      tasks = [];
    } else {
      tasks = JSON.parse(localStorage.getItem('tasks'));
    }

    tasks.forEach(function(task){
       // Create the li element for the new task
  const li = document.createElement('li');
  // Add a class to the new li element
  li.className = 'task-list-item';
  // Create a text node and append to the li
  li.appendChild(document.createTextNode(task));
    // Append the li to the list
  taskList.appendChild(li);

  // Create a link element for the delete button
  const link = document.createElement('a');
  // Add class to link
  link.classList = 'item-delete';
  // Add html into link
  link.innerHTML = '<img src="https://s33.postimg.cc/qhcxae4db/delete-button.gif" width=22px>'
  // Append the link to the list item
  li.appendChild(link);
    });
}


function addTask(e) {

  if (taskInput === "") {
    return;
    // Could add an alert in here that goes below the input field
  }

  // Create the li element for the new task
  const li = document.createElement('li');
  // Add a class to the new li element
  li.className = 'task-list-item';
  // Create a text node and append to the li
  li.appendChild(document.createTextNode(taskInput.value));
    // Append the li to the list
  taskList.appendChild(li);

  // Create a link element for the delete button
  const link = document.createElement('a');
  // Add class to link
  link.classList = 'item-delete';
  // Add html into link
  link.innerHTML = '<img src="https://s33.postimg.cc/qhcxae4db/delete-button.gif" width=22px>'
  // Append the link to the list item
  li.appendChild(link);

  // Add task to local storage
  storeTaskInLocalStorage(taskInput.value);

  // Clear the input field
  taskInput.value = '';

  e.preventDefault();
}

// Store task in local storage
function storeTaskInLocalStorage(task) {
  let tasks;
  if(localStorage.getItem('tasks') === null){
    tasks = [];
  } else {
    tasks = JSON.parse(localStorage.getItem('tasks'));
  }
  tasks.push(task);

  localStorage.setItem('tasks', JSON.stringify(tasks));
}


// Remove task from list
function removeTask(e) {
  if(e.target.parentElement.classList.contains('item-delete')){
    e.target.parentElement.parentElement.remove();

    // Remove item from local storage
    removeTaskFromLocalStorage(e.target.parentElement.parentElement);
  }
}

// Remove task from local storage
function removeTaskFromLocalStorage(taskItem){
  let tasks;
    if(localStorage.getItem('tasks') === null){
      tasks = [];
    } else {
      tasks = JSON.parse(localStorage.getItem('tasks'));
    }

    tasks.forEach(function(task, index){
      if(taskItem.textContent === task){
        tasks.splice(index, 1);
      }
    });

    localStorage.setItem('tasks', JSON.stringify(tasks));
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console