Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67497

How to pass unique variables to eventsListiner function?

$
0
0

I have a hard time adding events to object and array So:
-i created divs using createElement, added the same class using FOR loop and then appendChild to some other html TAG
-i want to addEvennt to divs and i want to pass unique variables(i from FOR loop)
-it returns i max value; or error or does nothing at all
THX for any kind of answer :D i know this is simple question but i can't find answer to this anywhere

window.onload = function (){

var sectionHTML = document.getElementById('section');

for(i=0;i<5;i++)
{
 var div_box = document.createElement('div');
 div_box.className = "box";
 div_box.innerText=i;
 sectionHTML.appendChild(div_box);
}
//adding Events
var getDIV = document.getElementsByClassName('box');
for(i=0;i<5;i++)
{
 getDiv[i].addEventListiner("click", function (i) { myFunction(i); })

}

}
myFunction (i) {
 var myString = "PLS HELP THX BRO or SIS " + i;
 alert(myString);
}










Viewing all articles
Browse latest Browse all 67497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>