JSFiddle - React, Tailwind, and code Playground

by erika_sun

HTML

<input id='input'/>
 <button onclick="add()">新增</button>
 <select>
  <option value="normal">一般</option>
  <option value="important">重要</option>
  <option value="urgent">緊急</option>
</select>
<div id="root">
 
</div>

CSS

.normal{
  color:black
}
.important{
  color:orange
}
.urgent{
  color:red
}

JavaScript

var todos = [
  {
    title: "倒垃圾",
    category: "normal"
  },
  {
    title: "繳電話費",
    category: "important"
  },
  {
    title: "採買本週食材",
    category: "urgent"
  },
];




function render()
{
    console.log('todos', todos)
    const root = document.querySelector('#root')
    const ul = document.createElement('ul')
 	root.textContent = ''
     root.append(ul)
     for(  let  index in  todos ){
     const li = document.createElement('li')
     const span = document.createElement('span')
     const delBtn = document.createElement('button')
     delBtn.textContent = '刪除'
    
       span.textContent = todos[index].title
       span.classList.add(todos[index].category)
       li.append(span)
       span.append(delBtn)
       ul.append(li)
       
       
       delBtn.onclick = ()=>{
          console.log('index',index)
          todos.splice(index, 1)
          render()
       }
     }
}

function add(){
	  const input = document.querySelector('input')
    const select = document.querySelector('select')
    console.log('select  ==> ', select.value)
    todos.push({ 
    title: input.value,
    category:select.value
    })
    input.value = ''
    render();
   
}

render();