
Event objectaddEventListener()메서드를 통해 Event 처리기(Event handler)를 다양한 html 요소에 “부착”해서 처리함<button id="btn">버튼</button>
<p id="counter">0</p>
// 초기값
let counterNumber = 0
// ID가 btn인 요소를 선택
const btn = document.querySelector('#btn')
// btn이 클릭 되었을 때마다 함수가 실행됨
btn.addEventListener('click', function() {
console.log('버튼 클릭!')
// countNumber를 증가시키고
counterNumber += 1
// id가 counter인 것의 내용을 변경
const counter = document.querySelector('#counter')
counter.innerText = counterNumber
})

btn.addEventListener('click', function(event) {
console.log(event) // console 창에서 어떤 event가 생성되는지 알 수 있음
})

<input type="text" id="text-input">
// 1. input 선택
const textInput = document.querySelector('input')
// 2. input 이벤트 등록
textInput.addEventListener('input', function(event) {
console.log(event)
// input은 이벤트의 대상
console.log(event.target)
// input의 value를 받아오기
console.log(event.target.value)
// 3. input에 작성한 값을 p 태그에 출력하기
const pTag = document.querySelector('p')
pTag.innerText = event.target.value
})

<h1></h1>
<button id="btn">클릭</button>
<input type="text">
const btn = document.querySelector('#btn')
// btn이 클릭되면 함수 실행
btn.addEventListener('click', function() {
// h1 태그 선택해서
const h1 = document.querySelector('h1')
// 클래스 blue를 토글하기
h1.classList.toggle('blue')
})
// input
const input = document.querySelector('input')
// input에 값이 입력되면 함수 실행
input.addEventListener('input', function(event) {
// h1 태그를 선택해서
const h1Tag = document.querySelector('h1')
// input 값을 태그의 컨텐츠로 채우기
h1Tag.innerText = event.target.value
})

addEventListner 메서드를 사용하여 전파 방식을 제어할 수 있음
<div id="divTag">
DIV영역
<p id="pTag">
P영역
<span id="spanTag">SPAN영역</span>
</p>
</div>
<section id="console"><br></section>
const divTag = document.querySelector('#divTag')
const pTag = document.querySelector('#pTag')
const spanTag = document.querySelector('#spanTag')
const consoleSection = document.querySelector('#console')
divTag.addEventListener('click', function () {
const message = document.createElement("li")
message.innerText = "div 클릭"
consoleSection.append(message)
// consoleSection.innerHTML += "<br> div 클릭"
})
pTag.addEventListener('click', function () {
// consoleSection.innerHTML += "<br> p 클릭"
const message = document.createElement("li")
message.innerText = "p 클릭"
consoleSection.append(message)
})
spanTag.addEventListener('click', function () {
const message = document.createElement("li")
message.innerText = "span 클릭"
consoleSection.append(message)
// 상위로 이벤트가 전파되지 않도록 중단한다.
event.stopPropagation();
// 상위 뿐 아니라 같은 레벨로도 이벤트가 전파되지 않도록 중단한다.
event.stopImmediatePropagation();
})
// span 태그에 2번째 이벤트 등록
spanTag.addEventListener('click', function () {
const message = document.createElement("li")
message.innerText = "span 클릭2"
consoleSection.append(message)
// 상위로 이벤트가 전파되지 않도록 중단한다.
event.stopPropagation();
})


event.stopPropagation(); 사용으로 상위로 이벤트가 전파 되지 않음
event.stopImmediatePropagation(); 을 안쓰면?
event.preventDefault()<div>
<h1>정말 중요한 내용</h1>
</div>
const h1 = document.querySelector('h1')
h1.addEventListener('copy', function (event) {
console.log(event)
// copy event의 기본 동작을 막기
event.preventDefault()
alert('복사 할 수 없습니다.')
})

<h1>로또 추천 번호</h1>
<button id="lotto-btn">행운 번호 받기</button>
<div id="result"></div>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
// 버튼을 클릭하면 로또 번호 뽑아주기
const button = document.querySelector('#lotto-btn')
button.addEventListener('click', function () {
// ball 컨테이너를 만들고
const ballContainer = document.createElement('div')
ballContainer.classList.add('ball-container')
// 랜덤 숫자 6개 만들기
const numbers = _.sampleSize(_.range(1, 46), 6)
// console.log(numbers) [3, 19, 12, 22, 6, 29]
// 공 만들기
numbers.forEach(number => {
const ball = document.createElement('div')
ball.classList.add('ball')
ball.innerText = number
ball.style.backgroundColor = 'crimson'
// ball을 ballContainer 자식으로 추가
ballContainer.appendChild(ball)
})
// ball 6개가 ballContainer에 들어가있음
// 컨테이너를 결과 영역의 자식으로 추가
const result = document.querySelector('#result')
result.appendChild(ballContainer)
})

<form action="#">
<input type="text" class="inputData">
<input type="submit" value="Add">
</form>
const formTag = document.querySelector('form')
const addTodo = function (event) {
event.preventDefault() // form 제출 기능 막기
const inputTag = document.querySelector('.inputData')
const data = inputTag.value
// trim() : 문자열 양 끝의 공백을 제거하는 메서드
// 데이터가 공백이 아닐 때만 실행하도록 조건문 작성
if (data.trim()) {
const liTag = document.createElement('li')
liTag.innerText = data
const ulTag = document.querySelector('ul')
ulTag.appendChild(liTag)
event.target.reset()
} else {
alert('할일을 입력하세요.')
}
}
formTag.addEventListener('submit', addTodo)


<button id="function">function</button>
<button id="arrow">arrow function</button>
const functionButton = document.querySelector('#function')
const arrowButton = document.querySelector('#arrow')
functionButton.addEventListener('click', function(event) {
console.log(this) // <button id="function">function</button>
})
arrowButton.addEventListener('click', event => {
console.log(this) // window
})
https://github.com/mjieun0956/TIL/tree/master/JavaScript/07.%20Event