How to use addEventListener in JavaScript?

In JavaScript, you can use the addEventListener method to add an event listener. The method takes a string which is the name of an event as its first argument. If it returns false, it is assumed that no action should be taken and nothing will be executed. You can use the addEventListener() function to attach a listener function to an event handler. Here is an example: 

js addeventlistener

By MAKSTYLE119MAKSTYLE119 on May 02, 2021
document.getElementById("example").addEventListener('click', function() {
  alert('Click');
});

Add Comment

8

javascript addeventlistener

By Innocent IbisInnocent Ibis on May 20, 2020
var element = document.getElementById("#id");
element.addEventListener('click', function(){
	console.log("click");
});

Add Comment

9

event listener javascript

By Levaillant's BarbetLevaillant's Barbet on Dec 31, 2019
const element = document.querySelector(".class__name");

element.addEventListener("click", () => {
	console.log("clicked element");
});

Add Comment

24

js addeventlistener

By GrepperGrepper on Jun 21, 2019
var element=document.getElementById("some_id");
var listener=element.addEventListener('click',function(event){                          
            
 }); 

Add Comment

12

javascript addeventlistener

By Innocent IbisInnocent Ibis on May 20, 2020
//with jQuery
$("#id / .class").on('click', function(){
	console.log("click");
});

Add Comment

1

addEventListener

By Exuberant EelExuberant Eel on May 19, 2020
<body style="height: 5000px">
 <style>
    body, /* blink currently has bug that requires declaration on `body` */
    html {
      scroll-snap-type: y proximity;
    }
    .snaptarget {
      scroll-snap-align: start;
      position: relative;
      top: 200px;
      height: 200px;
      background-color: green;
    }
 </style>
 <div class="snaptarget"></div>
</body>

Source: developer.mozilla.org

Add Comment

1

This method also takes a function which has an event object as its second argument and should return true if the function really wants to be called.

Javascript answers related to "js addeventlistener"

View All Javascript queries

Javascript queries related to "js addeventlistener"

Browse Other Code Languages

CodeProZone