동일한 요소에 둘 이상의 이벤트 핸들러를 추가하려면 addEventListener()를 두 번 이상 사용하십시오. 각 이벤트 핸들러에 대해 addEventListener() 메소드를 추가해야 합니다.
예시
둘 이상의 이벤트 핸들러를 추가하는 방법을 배우기 위해 다음 코드를 실행할 수 있습니다 -
<!DOCTYPE html>
<html>
<body>
<p>Click the below button once to get Date.</p>
<p>Click the below button twice to get an alert box.</p>
<button id="btnid"> Click me </button>
<p id="pid"></p>
<script>
document.getElementById("btnid").addEventListener("click", displayEvent);
var res = document.getElementById("btnid");
res.addEventListener("dblclick", myFunction);
function displayEvent() {
document.getElementById("pid").innerHTML = Date();
}
function myFunction() {
alert ("You clicked the button twice!");
}
</script>
</body>
</html>