<!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>
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.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme