Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vanilla TodoList</title>

    <style>
        .wrapper{
            display:flex;
            flex-direction:column;
            align-items:center;
            height:100vh;
        }

        h1{
            margin:50px;
            
        }
     
    </style>
</head>

<body>
    <div class="wrapper">
        <h1>Vanilla TodoList</h1>
    
        <div>
            <!-- <form id="form" > -->
                <input id="input" type="text" placeholder="add a todo">
                <button type="submit" id="btn">Add</button>
                <button id="removeAll">Remove All</button>
            <!-- </form> -->
        </div>
        <br>
   
        <ul>
     
        </ul>
    </div>

<!--     <script src="todo2.js"></script> -->
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                
let input = document.getElementById('input')
let btn = document.getElementById('btn')
let removeAllBtn = document.getElementById('removeAll')
let todosUl = document.querySelector('ul')
let todoList = document.querySelector('.wrapper')


let todos = []

// add
function add(todo){
    todos.push(todo)
    console.log(todos);
}

// add to dom
function addDom(){
  
    if(todosUl)
    {
        todosUl.innerHTML = " ";
    }
  
 // for each item in the todos array, i want to render it on the DOM.
    todos.forEach((todo,i)=>{
        let item = document.createElement('li')
        item.innerHTML=`${todo} <button class="edit">edit</button><button class="toggle">toggle</button><button class="delete">x</button>`
        item.id=i
        todosUl.appendChild(item)
    })
}

//add using Enter

input.addEventListener('keypress',(e) => {

    if(e.key =="Enter"){
        if(input.value){
            add(input.value)
            addDom()
            input.value="";
        } else {
            alert('do not submit an empty todo')
        }
    }
})


// add button listener
btn.addEventListener('click',(e)=>{
    if(input.value){
        add(input.value)
        addDom()
        input.value="";
    } else {
        alert('do not submit an empty todo')
    }
})


todoList.addEventListener('click',removeTodo)



// remove 
function removeTodo(e){

if (e.target.classList.contains('delete')){
    let parentId= e.target.parentNode.id
    console.log(e.target.parentNode.id)
    // e.target.parentNode.remove()
    console.log(e)
    document.getElementById(parentId).remove()
    todos.splice(parentId,1)
  
    //reset the id of the todo on dom so it matches its id in the todos array
    addDom()
  }
}

// todoList.addEventListener('click',reset)
removeAllBtn.addEventListener('click',removeAll)

function removeAll(){
    todos.splice(0,todos.length)
    if (todosUl){
        todosUl.innerHTML = ''
    }
}



// V2. TOGGLE LINETHROUGH

todoList.addEventListener('click',toggle)
                          
function toggle(e){
if (e.target.classList.contains('toggle')){
  e.target.classList.remove('toggle')
    let parentId= e.target.parentNode.id
    console.log(e.target.parentNode.id)
    document.getElementById(parentId).style.textDecoration = "line-through"
  }  else {
  e.target.classList.add('toggle')
    let parentId= e.target.parentNode.id
    console.log(e.target.parentNode.id)
    document.getElementById(parentId).style.textDecoration = "none"
  }
}

// V2. EDIT
todoList.addEventListener('click',edit)

function edit(e){
    if (e.target.classList.contains('edit')){
        let parentId= e.target.parentNode.id
        console.log(e.target.parentNode.id)
        var change = prompt("Change to do: ")
        if(change != null){
            document.getElementById(parentId).innerHTML = `${change} <button class="edit">edit</button><button class="toggle">toggle</button><button class="delete">x</button>`
            todos.splice(parentId,1,change)
        }
      }
      console.log(todos)
    }

              
            
!
999px

Console