LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

基礎学習 JavaScript編 実践 TodoApp

Last updated at Posted at 2020-09-02
<!DOCTYPE html>
<html lang="jp">

<head>
    <meta charset="UTF-8">
    <title>JS App</title>
</head>

<body>
<input type="text" id="todo-title" placeholder="Todoを入力して下さい">
<button id="btn-todo">
    追加
</button>

<p id="todo-container"></p>

<script>
    const inputTodoTitle = document.getElementById("todo-title");
    const buttonTodo = document.getElementById("btn-todo");
    const containerTodo = document.getElementById("todo-container");

    // 追加ボタンをクリックしたら実行されるイベントリスナーを設定
    buttonTodo.addEventListener('click', e = > {

    console.log("btnTodo.addEventListenerが起動しました");

    // submitイベントのデフォルト動作(ページリロードなど)を実行しないよう設定
    e.preventDefault();

    // inputタグのオブジェクトが取得されているかを確認
    console.log("inputTodoTitle : " + inputTodoTitle);

    // inputタグに入力された値を取得
    const addeTodo = inputTodoTitle.value;
    console.log("入力されたのは : " + addeTodo);

    // 入力された値をcontainerTodoのinnerTextプロパティに代入しなさい。
    containerTodo.insertAdjacentHTML('beforeend', addeTodo);
    })
</script>
</body>
</html>
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up