I'm trying to trigger 'for loop' by clicking, with 'addEventListener' function. I've seen lots of questions asking how to make addEventListener embedded in for loop, but couldn't find a case of the opposite, for loop embedded in addEventListener.
This is the html -----html-----
<div id="myDiv">
<p id="myP">HO HO HO</p>
</div>
-----JavaScript-----
var myDiv = document.getElementById("myDiv");
var objs = {Aman : function(){myDiv.style.background="yellow"},
Bman : function(){myDiv.style.margin = "30px"},
Cman : function(){myDiv.style.border ="blue"}};
function loop(){
for(var i=0; i < objs.length ; i++){
return objs[Object.keys(objs)[i]] }
};
myDiv.addEventListener("click", loop);
I really can't find why this doesn't work. Really appreciate for you genius.
p.s; If this can't work, then how can I link more than one events to just one 'addEventListener' phrase?
for(var i=0; i < objs.length ; i++){is incorrect. You can get theObjectlength usingObject.keys.length.