티스토리 뷰

 

 

 

form 태그에서 input또는 button 클릭 이벤트 발생 시 페이지가 reload 된다.
이것을 방지하기 위한 방법은 event.preventDefault()를 사용하는 것이다.

 

event.preventDefault() 는 브라우저의 기본 동작을 막아주는 역할을 한다.

 


 
html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Momentum APP</title>
</head>
<body>
  <form id="todo-form">
    <input type="text" placeholder="Wirte a To Do and Press Enter"  required />
  </form>
  <ul id="todo-list"></ul>
  <script src="todo.js"></script>
</body>
</html>

 

 

js

const toDoForm = document.getElementById("todo-form");
const toDoList = document.getElementById("todo-list");


function handleToDoSubmit(event){
    event.preventDefault();
}

toDoForm.addEventListener("submit", handleToDoSubmit);
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함