요소에 이벤트 핸들러를 추가하려면 JavaScript addEventListener() 방법. 다음 코드를 실행하여 이벤트 핸들러를 추가할 수 있습니다. -
예시
<!DOCTYPE html>
<html>
<body>
<p>Click the below button.</p>
<button id = "btnid">Click me</button>
<p id = "pid"></p>
<script>
document.getElementById("btnid").addEventListener("click", displayEvent);
function displayEvent() {
document.getElementById("pid").innerHTML = Date();
}
</script>
</body>
</html>