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 class="container">
  <div class="row justify-content-center align-items-center main-row">
    <div class="col shadow main-col bg-white">
      <div class="row bg-primary text-white">
        <div class="col  p-2">
          <h4>Todo App</h4>
        </div>
      </div>
      <div class="row justify-content-between text-white p-2">
        <div class="form-group flex-fill mb-2">
          <input id="todo-input" type="text" class="form-control" value="">
        </div>
        <button type="button" onclick="CreateTodo();" class="btn btn-primary mb-2 ml-2">Add todo</button>
      </div>
      <div class="row" id="todo-container">
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                .main-row{
    height: 100vh;
}
.main-col{
    max-width: 500px;
    min-height: 300px;
}
.todo-done{
    text-decoration: line-through;
}
              
            
!

JS

              
                var todos = [{text: "take out the trash",
    done: false,
    id: 0
    }];

var currentTodo = {
    text: "",
    done: false,
    id: 0
}

document.getElementById("todo-input").oninput = function(e){
  currentTodo.text = e.target.value;
};

/*
//jQuery Version
$('#todo-input').on('input',function(e){
    currentTodo.text = e.target.value;
   });
*/

function DrawTodo(todo) {
  var newTodoHTML =`
  <div class="col col-12 p-2 todo-item" todo-id="${todo.id}">
  <div class="input-group">
  <div class="input-group-prepend">
    <div class="input-group-text">
      <input type="checkbox" onchange="TodoChecked(${todo.id})" aria-label="Checkbox for following text input" ${todo.done&&"checked"} >
    </div>
  </div>
  <input type="text" readonly class="form-control ${todo.done&&"todo-done"} " aria-label="Text input with checkbox"
    value="${todo.text}">
  <div class="input-group-append">
    <button todo-id="${todo.id}" class="btn btn-outline-secondary bg-danger text-white" type="button" onclick="DeleteTodo(this);"
      id="button-addon2 ">X</button>
  </div>
  </div>
  </div>
  `;

    var dummy = document.createElement("DIV");
    dummy.innerHTML = newTodoHTML;
    document.getElementById("todo-container").appendChild(dummy.children[0]);
    
    /*
    //jQuery version
     var newTodo = $.parseHTML(newTodoHTML);
     $("#todo-container").append(newTodo);
    */

}

function RenderAllTodos() {

    var container = document.getElementById("todo-container");
    while (container.firstChild) {
      container.removeChild(container.firstChild);
    }
    /*
    //jQuery version
      $("todo-container").empty();
    */


    for (var i = 0; i < todos.length; i++) {
      DrawTodo(todos[i]);
    }
}

RenderAllTodos();

function DeleteTodo(button){

    var deleteID = parseInt(button.getAttribute("todo-id"));
    /*
    //jQuery version
      var deleteID = parseInt($(button).attr("todo-id"));
    */

    for(let i=0;i<todos.length;i++){
        if(todos[i].id === deleteID){
            todos.splice( i, 1 );
            RenderAllTodos();
            break;
        }
    }
}

function TodoChecked(id){
    todos[id].done = !todos[id].done;
    RenderAllTodos();
}

function CreateTodo(){
    newtodo ={
       text:currentTodo.text,
       done:false,
       id:todos.length 
    }
    todos.push(newtodo);
    RenderAllTodos();
}
              
            
!
999px

Console