Search code examples
javascripthtmlcssbuttoncheckbox

Checkbox symbol for a to do list in Javascript


I'm trying to create a to-do list application in JavaScript. Using HTML and CSS, I've created a checkmark button. The problem is that the checkmark button doesn't display when adding a new task, but the task is added. Can someone explain to me what's wrong and how it should be done?

I can not understand if my problem is from HTML or CSS or JavaScript.

Code Snippet

let userinput = [];

function task() {
  const inputText = document.getElementById("inputbutton").value;

  // Check if inputText already exists in userinput array
  if (userinput.includes(inputText)) {
    alert("Already added");
    return; // Exit function early if inputText already exists
  }


  userinput.push(inputText);

  let tasklist = document.getElementById("tasklist");
  let listItem = document.createElement("li");
  listItem.textContent = inputText;
  tasklist.appendChild(listItem);

}

/*
I also tried this:
   let checkmarks = document.querySelectorAll('.container .checkmark');
    checkmarks.forEach(checkmark => {
        checkmark.style.display = 'block';
    });
*/
* {
  box-sizing: border-box;
  margin: 0;
}

#inputbutton {
  color: hsl(210deg 100% 44%);
  ;
  border: 2px solid hsl(210deg 100% 44%);
  ;
  border-radius: 10px;
  padding: 10px 25px;
  background: transparent;
  max-width: 190px;
  margin-top: 4em;
  margin-left: 42em;
  display: inline-flex;
  position: relative;
}

ul li {
  list-style: none;
  font-size: 17px;
  padding: 12px 8px 50px;
  user-select: none;
  cursor: pointer;
}

.input:active {
  box-shadow: 2px 2px 15px #8707ff inset;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  position: relative;
  padding: 0px 11px;
  font-size: 18px;
  text-transform: uppercase;
  border: 0;
  background-color: hsl(210deg 100% 44%);
  border-radius: 12px;
  overflow: hidden;
  transition: 31ms cubic-bezier(.5, .7, .4, 1);
}

button:before {
  content: attr(alt);
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  inset: 0;
  font-size: 15px;
  font-weight: bold;
  color: white;
  letter-spacing: 4px;
  opacity: 1;
}

button:active {
  box-shadow: none;
  transform: translateY(7px);
  transition: 35ms cubic-bezier(.5, .7, .4, 1);
}

button:hover:before {
  transition: all .0s;
  transform: translateY(100%);
  opacity: 0;
}

button i {
  color: white;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 4px;
  font-style: normal;
  transition: all 2s ease;
  transform: translateY(-20px);
  opacity: 0;
}

button:hover i {
  transition: all .2s ease;
  transform: translateY(0px);
  opacity: 1;
}

button:hover i:nth-child(1) {
  transition-delay: 0.045s;
}

button:hover i:nth-child(2) {
  transition-delay: calc(0.045s * 3);
}

button:hover i:nth-child(3) {
  transition-delay: calc(0.045s * 4);
}

button:hover i:nth-child(4) {
  transition-delay: calc(0.045s * 5);
}

button:hover i:nth-child(6) {
  transition-delay: calc(0.045s * 6);
}

button:hover i:nth-child(7) {
  transition-delay: calc(0.045s * 7);
}

button:hover i:nth-child(8) {
  transition-delay: calc(0.045s * 8);
}

button:hover i:nth-child(9) {
  transition-delay: calc(0.045s * 9);
}

button:hover i:nth-child(10) {
  transition-delay: calc(0.045s * 10);
}


/* Hide the default checkbox */

.container input {
  display: none;
}

.container {
  display: block;
  position: relative;
  cursor: pointer;
  font-size: 20px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}


/* Create a custom checkbox */

.checkmark {
  position: relative;
  top: 0;
  left: 0;
  height: 1.3em;
  width: 1.3em;
  background-color: #2196F300;
  border-radius: 0.25em;
  transition: all 0.25s;
}


/* When the checkbox is checked, add a blue background */

.container input:checked~.checkmark {
  background-color: #2196F3;
}


/* Create the checkmark/indicator (hidden when not checked) */

.checkmark:after {
  content: "";
  position: absolute;
  transform: rotate(0deg);
  border: 0.1em solid black;
  left: 0;
  top: 0;
  width: 1.05em;
  height: 1.05em;
  border-radius: 0.25em;
  transition: all 0.25s, border-width 0.1s;
}


/* Show the checkmark when checked */

.container input:checked~.checkmark:after {
  left: 0.45em;
  top: 0.25em;
  width: 0.25em;
  height: 0.5em;
  border-color: #fff0 white white #fff0;
  border-width: 0 0.15em 0.15em 0;
  border-radius: 0em;
  transform: rotate(45deg);
}
<input tyoe="text" id="inputbutton"></input>
<button class="addtask" onclick="task()" alt="Add task">
        <i>A</i>
        <i>d</i>
        <i>d</i>
        <i>&nbsp;</i>
        <i>t</i>
        <i>a</i>
        <i>s</i>
        <i>k</i>
      </button>
<label class="container">
        <input type="checkbox"/>
        <div class="checkmark"></div>
      </label>
<ul id="tasklist"></ul>


Solution

  • A todo list is a classic, and also this technique for duplicating templates. Just copy the html and set the innerHTML of the target.

    let userinput = [];
    
    
    function task() {
      const inputText = document.getElementById("inputbutton").value;
    
      // Check if inputText already exists in userinput array
      if (userinput.includes(inputText)) {
        alert("Already added");
        return; // Exit function early if inputText already exists
      }
    
      var html = document.querySelector("#myTaskTemplate").innerHTML
    
      userinput.push(inputText);
    
      let tasklist = document.getElementById("tasklist");
      let listItem = document.createElement("li");
      // listItem.textContent = inputText;
      listItem.innerHTML = html
      
      // after applying html, you can adjust elements or attach events
      listItem.querySelector(".checkmark").textContent = inputText
    
      tasklist.appendChild(listItem);
    
    }
    
    /*
    I also tried this:
       let checkmarks = document.querySelectorAll('.container .checkmark');
        checkmarks.forEach(checkmark => {
            checkmark.style.display = 'block';
        });
    */
    * {
      box-sizing: border-box;
      margin: 0;
    }
    
    #inputbutton {
      color: hsl(210deg 100% 44%);
      border: 2px solid hsl(210deg 100% 44%);
      border-radius: 10px;
      padding: 10px 25px;
      background: transparent;
      max-width: 190px;
      margin-top: 4em;
      margin-left: 42em;
      display: inline-flex;
      position: relative;
    }
    
    ul li {
      list-style: none;
      font-size: 17px;
      padding: 12px 8px 50px;
      user-select: none;
      cursor: pointer;
    }
    
    .input:active {
      box-shadow: 2px 2px 15px #8707ff inset;
    }
    
    button {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      height: 40px;
      position: relative;
      padding: 0px 11px;
      font-size: 18px;
      text-transform: uppercase;
      border: 0;
      background-color: hsl(210deg 100% 44%);
      border-radius: 12px;
      overflow: hidden;
      transition: 31ms cubic-bezier(.5, .7, .4, 1);
    }
    
    button:before {
      content: attr(alt);
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      inset: 0;
      font-size: 15px;
      font-weight: bold;
      color: white;
      letter-spacing: 4px;
      opacity: 1;
    }
    
    button:active {
      box-shadow: none;
      transform: translateY(7px);
      transition: 35ms cubic-bezier(.5, .7, .4, 1);
    }
    
    button:hover:before {
      transition: all .0s;
      transform: translateY(100%);
      opacity: 0;
    }
    
    button i {
      color: white;
      font-size: 15px;
      font-weight: bold;
      letter-spacing: 4px;
      font-style: normal;
      transition: all 2s ease;
      transform: translateY(-20px);
      opacity: 0;
    }
    
    button:hover i {
      transition: all .2s ease;
      transform: translateY(0px);
      opacity: 1;
    }
    
    button:hover i:nth-child(1) {
      transition-delay: 0.045s;
    }
    
    button:hover i:nth-child(2) {
      transition-delay: calc(0.045s * 3);
    }
    
    button:hover i:nth-child(3) {
      transition-delay: calc(0.045s * 4);
    }
    
    button:hover i:nth-child(4) {
      transition-delay: calc(0.045s * 5);
    }
    
    button:hover i:nth-child(6) {
      transition-delay: calc(0.045s * 6);
    }
    
    button:hover i:nth-child(7) {
      transition-delay: calc(0.045s * 7);
    }
    
    button:hover i:nth-child(8) {
      transition-delay: calc(0.045s * 8);
    }
    
    button:hover i:nth-child(9) {
      transition-delay: calc(0.045s * 9);
    }
    
    button:hover i:nth-child(10) {
      transition-delay: calc(0.045s * 10);
    }
    
    
    /* Hide the default checkbox */
    
    .container input {
      display: none;
    }
    
    .container {
      display: block;
      position: relative;
      cursor: pointer;
      font-size: 20px;
      user-select: none;
      -webkit-tap-highlight-color: transparent;
    }
    
    
    /* Create a custom checkbox */
    
    .checkmark {
      position: relative;
      top: 0;
      left: 0;
      height: 1.3em;
      width: 1.3em;
      background-color: #2196F300;
      border-radius: 0.25em;
      transition: all 0.25s;
    }
    
    
    /* When the checkbox is checked, add a blue background */
    
    .container input:checked~.checkmark {
      background-color: #2196F3;
    }
    
    
    /* Create the checkmark/indicator (hidden when not checked) */
    
    .checkmark:after {
      content: "";
      position: absolute;
      transform: rotate(0deg);
      border: 0.1em solid black;
      left: 0;
      top: 0;
      width: 1.05em;
      height: 1.05em;
      border-radius: 0.25em;
      transition: all 0.25s, border-width 0.1s;
    }
    
    
    /* Show the checkmark when checked */
    
    .container input:checked~.checkmark:after {
      left: 0.45em;
      top: 0.25em;
      width: 0.25em;
      height: 0.5em;
      border-color: #fff0 white white #fff0;
      border-width: 0 0.15em 0.15em 0;
      border-radius: 0em;
      transform: rotate(45deg);
    }
    <input type="text" id="inputbutton">
    <button class="addtask" onclick="task()" alt="Add task">
      <i>A</i>
      <i>d</i>
      <i>d</i>
      <i>&nbsp;</i>
      <i>t</i>
      <i>a</i>
      <i>s</i>
      <i>k</i>
    </button>
    <div id="myTaskTemplate" style="display:none">
      <label class="container">
        <input type="checkbox" />
        <div class="checkmark"></div>
      </label>
    </div>
    <ul id="tasklist"></ul>