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

              
                <body class="bg-dark">
  <div class="container mt-5">
    <div class="row justify-content-center">
      <div class="col-md-6">
        <div class="input-group mb-3">
          <input id="dataField" class="form-control py-3" type="text" />
          <select id="category" class="form-select flex-grow-0 w-auto py-3">
            <option value="normal">一般</option>
            <option value="important">重要</option>
            <option value="urgent">緊急</option>
          </select>
          <button class="btn btn-primary py-3" onclick="add()">新增</button>
        </div>
        <div class="card p-3">
          <button class="btn btn-secondary align-self-end mb-3" onclick="exportText()">
            匯出
          </button>
          <div id="root"></div>
        </div>
      </div>
    </div>
  </div>
</body>
              
            
!

CSS

              
                
              
            
!

JS

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

function render() {
  const root = document.querySelector("#root");
  root.textContent = "";

  const todolist = document.createElement("ul");
  todolist.className = "list-unstyled";

  for (const index in todos) {
    const todo = document.createElement("li");
    const content = document.createElement("span");
    const done = document.createElement("span");
    const toggleBtn = document.createElement("button");
    const delBtn = document.createElement("button");

    todo.className =
      "form-control d-flex align-items-center border-secondary mb-3";

    content.textContent = todos[index].title;

    if (todos[index].category == "important") {
      content.style.color = "orange";
    }
    if (todos[index].category == "urgent") {
      content.style.color = "red";
    }

    done.textContent = "(已完成)";

    if (todos[index].isCompleted) {
      done.style.display = "inline-block";
      toggleBtn.textContent = "標示為未完成";
    } else {
      done.style.display = "none";
      toggleBtn.textContent = "標示為已完成";
    }

    toggleBtn.className = "btn btn-success ms-auto";
    toggleBtn.onclick = () => {
      if (todos[index].isCompleted) {
        todos[index].isCompleted = false;
      } else {
        todos[index].isCompleted = true;
      }

      render();
    };

    delBtn.textContent = "刪除";
    delBtn.className = "btn btn-danger ms-1";
    delBtn.onclick = () => {
      todos.splice(index, 1);
      render();
    };

    todo.append(content, done, toggleBtn, delBtn);
    todolist.append(todo);
  }

  root.append(todolist);
}

render();

function add() {
  const dataField = document.querySelector("#dataField");
  const category = document.querySelector("#category");

  if (!dataField.value) return alert("請輸入內容!!");

  todos.push({
    title: dataField.value,
    category: category.value,
    isCompleted: false
  });
  dataField.value = "";
  dataField.focus();

  render();
}

function exportText() {
  let result = "今日待辦:";

  todos.forEach((todo, index) => {
    if (todo.category == "important") {
      result += `${index + 1}. *${todo.title}* `;
    } else if (todo.category == "urgent") {
      result += `${index + 1}. **${todo.title}** `;
    } else {
      result += `${index + 1}. ${todo.title} `;
    }
  });

  alert(result);
}

              
            
!
999px

Console