html5 - Checking which element is causing the event from a list of elements in Javascript -


i have list of elements event listeners :

var h1 = document.getelementbyid("h1"),     h2 = document.getelementbyid("h2"),     h3 = document.getelementbyid("h3");  var elemarray = [h1,h2,h3];  for(var i=0;i<elemarray.length;i++){     elemarray[i].addeventlistener("click",mouselistener,false); }  function mouselistener(e){     // code goes here     // whenever it's called want know     // element it's been called } 

now want whenever click on 1 of elements,
i want know 1 has been clicked without using different functions each element.

just use this variable inside callback, point target element:

function mouselistener(e) {     console.log( this.id ); } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -