본문 바로가기

javaScript

[JS / javascript] addEventListener에 사용할 수 있는 문법

보통 이벤트리스너로 버튼을 클릭했을 때

 

~~ 이러한 코드를 실행 주세요 라는 기능을 많이 사용한다

 

클릭뿐 많이 아니라 이벤트리스너로 사용할 수 있는 예약어들이 있다 

 

내가 보려고 만드는 것이니 하나하나씩 추가하겠다

 

click

<script>
 // click
    document.getElementById('id').addEventListener('click', function(){
      //실행할 코드를 여기에 적어주세요
    });
</script>

예를 들면 버튼을 클릭했을 때 실행할 코드~

 

mouseover

<script>
 // mouseover
    document.getElementById('id').addEventListener('mouseover', function(){
      //실행할 코드를 여기에 적어주세요
    });
</script>

마우스를 요소에 스윽 가져다 놓았을 때 

정확히는 내가 찾은 id라는 요소 안으로 마우스가 들어왔을 때를 얘기함

 

input

<script>
 // mouseover
    document.getElementById('id').addEventListener('input', function(){
      //실행할 코드를 여기에 적어주세요
    });
</script>

input 창에 입력값이 들어왔을 때