Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add multiple event listeners

['click','ontouchstart'].forEach( function(evt) {
    element.addEventListener(evt, dosomething, false);
});
Comment

add click event to multiple items JavaScript

//HTML
 <body>
    <h1 id="title">Drum Kit</h1>
    <div class="set">
      <button class="w drum">w</button>
      <button class="a drum">a</button>
      <button class="s drum">s</button>
      <button class="d drum">d</button>
      <button class="j drum">j</button>
      <button class="k drum">k</button>
      <button class="l drum">l</button>
    </div>

    <script src="index.js"></script>
  </body>

//JavaScript
//number of elements with class of drum
var numOfDrumButtons = document.querySelectorAll('.drum').length;
//assigning the buttons to an array called drums
var drums = document.querySelectorAll('.drum');
//using a for loop to iterate through each arrayed element
for (var i = 0; i < numOfDrumButtons; i++) {
  //each array indexed and assigned an anonymous function with alert
  drums[i].addEventListener('click', function () {
    alert('I got clicked');//for testing
    //add actions to occur upon click hear
  });
}
Comment

addEventListener to multiple events

var eventList = ["change", "keyup", "paste", "input", "propertychange", "..."];
for(event of eventList) {
    element.addEventListener(event, function() {
        // your function body...
        console.log("you inserted things by paste or typing etc.");
    });
}
Comment

addeventlistener javascript multiple functions

const invokeMe = () => console.log('I live here outside the scope');
const alsoInvokeMe = () => console.log('I also live outside the scope'); 

element.addEventListener('event',() => {    
     invokeMe();
     alsoInvokeMe();    
});
Comment

javascript addeventlistener click multiple elements

let all_btn = document.querySelectorAll("button");
all_btn.forEach(function(btn) {
    btn.addEventListener("click", function() {
        console.log(this.innerHTML + " is clicked")
    });
});

// one line code
// let all_btn=document.querySelectorAll("button");all_btn.forEach(function(n){n.addEventListener("click",function(){console.log(this.innerHTML+" is clicked")})});
Comment

how to add multiple event listener in javascript

// events and args should be of type Array
function addMultipleListeners(element,events,handler,useCapture,args){
  if (!(events instanceof Array)){
    throw 'addMultipleListeners: '+
          'please supply an array of eventstrings '+
          '(like ["click","mouseover"])';
  }
  //create a wrapper to be able to use additional arguments
  var handlerFn = function(e){
    handler.apply(this, args && args instanceof Array ? args : []);
  }
  for (var i=0;i<events.length;i+=1){
    element.addEventListener(events[i],handlerFn,useCapture);
  }
}

function handler(e) {
  // do things
};

// usage
addMultipleListeners(
    document.getElementById('first'),
    ['touchstart','click'],
    handler,
    false);
Comment

addEventListener to multiple events

var eventList = ["change", "keyup", "paste", "input", "propertychange", "..."];
for(event of eventList) {
    element.addEventListener(event, function() {
        // your function body...
        console.log("you inserted things by paste or typing etc.");
    });
}
Comment

javascript add onclick to multiple elements

const arrayOfElements = document.querySelectorAll(".classNameToAddOnClickTo")
arrayOfElements.forEach(x => x.setAttribute("onclick", "methodToCall(this.id)"))
Comment

PREVIOUS NEXT
Code Example
Javascript :: count items in json 
Javascript :: selectores de jquery 
Javascript :: javascript array join last element with and 
Javascript :: how to change the text of a paragraph 
Javascript :: how to use promise.all 
Javascript :: javascript atan2 
Javascript :: how to create an html element in javascript without document 
Javascript :: adding event listener to multiple elements 
Javascript :: get the index of object in array 
Javascript :: .keys() array 
Javascript :: How to add click event to table row js 
Javascript :: print stuff in console javascript 
Javascript :: replace specific values in array 
Javascript :: eslint disable line 
Javascript :: angular mouseenter 
Javascript :: Html.Java script div content value change using id 
Javascript :: js document on load 
Javascript :: webpack vue global variable 
Javascript :: javascript addeventlistener click only works once 
Javascript :: javascript add to string 
Javascript :: javascript function with parameters 
Javascript :: decode jwt token without library 
Javascript :: restart bot discord.js 
Javascript :: pwa clear cache 
Javascript :: javascript, dynamic variable, and function to add data to O 
Javascript :: use the AJAX XMLHttpRequest object in Javascript to send json data to the server 
Javascript :: Use jsx extension react-native 
Javascript :: ?? javascript 
Javascript :: unexpected token react 
Javascript :: method example js 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =