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

              
                <div data-controller="todolist">
  <div id="myDIV" class="header">
    <h2 style="margin:5px">My To Do List with Stimulus.js</h2>
    <form data-action="submit->todolist#newElement">
      <input type="text" data-target="todolist.myinput" placeholder="Title..." tabindex="1">
      <input type="submit" value="Add" class="addBtn" tabindex="2" >
    </form>
  </div>

  <ul data-target="todolist.mylist">
    <li>Hit the gym</li>
    <li class="checked">Pay bills</li>
    <li>Meet George</li>
    <li>Buy eggs</li>
    <li>Read a book</li>
    <li>Organize office</li>
  </ul>

  <div><br><br>
    <label>Number of Tasks :</label>
    <span data-target="todolist.nbre">6</span><br>
    <label>Tasks done :</label>
    <span data-target="todolist.nbreal">x</span><br>
  </div>
</div><!-- End of data-controller="todolist" -->
              
            
!

CSS

              
                body {
  margin: 0;
  min-width: 250px;
}

/* Include the padding and border in an element's total width and height */
* {
  box-sizing: border-box;
}

/* Remove margins and padding from the list */
ul {
  margin: 0;
  padding: 0;
}

/* Style the list items */
ul li {
  cursor: pointer;
  position: relative;
  padding: 12px 8px 12px 40px;
  list-style-type: none;
  background: #eee;
  font-size: 18px;
  transition: 0.2s;

  /* make the list items unselectable */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Set all odd list items to a different color (zebra-stripes) */
ul li:nth-child(odd) {
  background: #f9f9f9;
}

/* Darker background-color on hover */
ul li:hover {
  background: #ddd;
}

/* When clicked on, add a background color and strike out text */
ul li.checked {
  background: #888;
  color: #fff;
  text-decoration: line-through;
}

/* Add a "checked" mark when clicked on */
ul li.checked::before {
  content: '';
  position: absolute;
  border-color: #fff;
  border-style: solid;
  border-width: 0 2px 2px 0;
  top: 10px;
  left: 16px;
  transform: rotate(45deg);
  height: 15px;
  width: 7px;
}

/* Style the close button */
.close {
  position: absolute;
  right: 0;
  top: 0;
  padding: 12px 16px 12px 16px;
}

.close:hover {
  background-color: #f44336;
  color: white;
}

/* Style the header */
.header {
  background-color: #f44336;
  padding: 30px 40px;
  color: white;
  text-align: center;
}

/* Clear floats after the header */
.header:after {
  content: "";
  display: table;
  clear: both;
}

/* Style the input */
input {
  border: none;
  width: 75%;
  padding: 10px;
  float: left;
  font-size: 16px;
}

/* Style the "Add" button */
.addBtn {
  padding: 10px;
  width: 25%;
  background: #d9d9d9;
  color: #555;
  float: left;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  transition: 0.3s;
}

.addBtn:hover {
  background-color: #bbb;
}
              
            
!

JS

              
                // https://www.w3schools.com/howto/howto_js_todolist.asp    

(() => {
  const application = Stimulus.Application.start()

  application.register("todolist", class extends Stimulus.Controller {
    static get targets() {
      return ["myinput", "mylist", "nbre", "nbreal"];
    }

    addCloseButtonOnLiItem() {
      var span = document.createElement("SPAN");
      var txt = document.createTextNode("\u00D7");
      span.className = "close";
      span.appendChild(txt);
      span.dataset['action'] = "click->todolist#dropItem" ; 

      return span;
    }

    newElement(evt) {
      evt.preventDefault();
      var input = this.myinputTarget;
      if (input && input.value !== '') {
        // Create a new list item when clicking on the "Add" button
        var li = document.createElement("li");
        var t = document.createTextNode(input.value);
        li.appendChild(t);
        let span = this.addCloseButtonOnLiItem();
        li.appendChild(span);
        li.dataset['action'] = this.initToggleCheck();
        this.mylistTarget.appendChild(li);
        this.myinputTarget.value = "";
        this.nbreTarget.textContent = parseInt(this.nbreTarget.textContent) + 1;

      }
    }

    addConfigOnLiItems() {
      var nbreal = 0;
      // Create a "close" button and append it to each list item
      var myNodelist = this.mylistTarget.children;
      for (let i = 0, imax = myNodelist.length; i < imax; i++) {
        let span = this.addCloseButtonOnLiItem();
        myNodelist[i].appendChild(span);
        myNodelist[i].dataset['action'] = this.initToggleCheck();
        myNodelist[i].tabIndex = 3;
        if (myNodelist[i].classList.contains('checked')) {
          nbreal += 1;
        }
      }
      this.nbrealTarget.textContent = nbreal;
    }

    initToggleCheck() {
      return "click->todolist#execToggleCheck";
    }

    execToggleCheck(evt) {
      var nbreal = parseInt(this.nbrealTarget.textContent);
      if (evt.target.classList.contains('checked')) {
        nbreal -= 1;
      } else {
        nbreal += 1;
      }
      this.nbrealTarget.textContent = nbreal;
      evt.target.classList.toggle('checked');
    }

    dropItem(evt) {
      if (evt.target.parentNode.classList.contains('checked')) {
        let nbreal = parseInt(this.nbrealTarget.textContent) - 1;
        this.nbrealTarget.textContent = nbreal;
      }
      let item = evt.target.parentNode;
      let parent = item.parentNode;
      parent.removeChild(item);
      let nbre = parseInt(this.nbreTarget.textContent) - 1;
      if (nbre < 0) {
        nbre = 0;
      }
      this.nbreTarget.textContent = nbre;

    }

    connect() {
      this.addConfigOnLiItems();
      this.myinputTarget.focus();
    }

  })
})()
              
            
!
999px

Console