How to call multiple JavaScript functions by using function in onclick event and using loop to count functions from array in function? Demonstration:
var arr = [function one(){console.log("one")}, function two(){console.log("two")} ];
htmlElement.onclick = function(){
for(var i = 0, a = arr.length; i < a; i++ ){
}
}
Simple - don't use onclick
- it's brittle (especially when used inline within HTML) and harks back to how JS was written in the '90s.
Use addEventListener
instead:
arr.forEach(function(callback) {
htmlElement.addEventListener('click', callback);
});
for(var i = 0,a = arr.length; i<a; i++){
arr[i]();
}
This should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.