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

              
                <html>
  <head>
    <title>My To-Do List</title>
  </head>
  <body>
    <h1>My To-Do List</h1>
    <form id="form">
      <input type="text" id="input" placeholder="Enter something to do">
      <button type="submit">Add</button>
    </form>
    <ul id="list"></ul>
  </body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                // Get a reference to the form and input elements
const form = document.querySelector("#form");
const input = document.querySelector("#input");
let todos = []; // create an empty array to store our todos
let listItems = []; // HTML list items for our list

const addItem = (item) => {    
  if(item){
    listItems += `<li class="task" id="${todos.length}">${item}</li>`;   
  }   
}

const updateList = () => {
  listItems = "";
  for(item of todos){    
    listItems += `<li class="task" id="${todos.length}">${item}</li>`;   
  }
  
  // Add the new items to the list
  document.getElementById("list").innerHTML = listItems;
}

// Remove a task from the to-do list
const removeTask = (id, item) => {
  // create a new array
  const newList = [];
  
  // loop through the todos and only add tasks that weren't removed to the new array
  for(task of todos) {
    if(task != item){
      newList.push(task);
    }
  }
  
  todos = newList; // assign the new list to the todos
  updateList();
}


const list = document.querySelector('ul');

// Listen for the clicks on the todo list
list.addEventListener("click", (event) => {  
  removeTask(event.target.id, event.target.innerText);
});
                      
// Listen for the form's submit event
form.addEventListener("submit", (event) => {
  // Prevent the default form submission behavior and keep the page from reloading
  event.preventDefault();

  // Get the value of the input field
  const item = input.value;

  // Create a new list item
  todos.push(item);
    
  // Updates the list items
  addItem(item);
  
  // Add the new item to the list
  document.getElementById("list").innerHTML = listItems;

  // Clear the input field
  input.value = "";
});

              
            
!
999px

Console