JSFiddle - React, Tailwind, and code Playground

by wimp9487

HTML

<input type="text" id="add-input">
<button onclick="add()">新增</button>
<div id="root">
</div>

JavaScript

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

function render() {
  const root = document.querySelector('#root');
  const ul = document.createElement('ul');
  root.textContent = "";
  root.append(ul);
  for(let item of todos){
   const li = document.createElement('li');
   const span = document.createElement('span');
   ul.append(li);
   li.append(span);
   span.textContent = item.title;
  }
}
render();
function add() {
  const addInput = document.querySelector('#add-input');
  const NeedValue = {title: addInput.value};
  todos.push(NeedValue);
  render();
  addInput.value = '';
};