I would like to create list of divs, one of the divs should like this:
<div onclick="myFunc(this, 'arrayValue')"></div>
I want to achieve it by iterating through my array and passing index value to my function. One of the solutions that worked was using setAttribute
:
for(var i = 0; i < array.length; i++){
var div = document.createElement("div");
div.setAttribute("onclick", 'myFunc(this, \''+array[i]+'\')');
myDiv.appendChild(div);
}
The only problem is that this part looks really ugly: \''+array[i]+'\')'
Is there other way to achieve this? That would look like this or something similar:
div.setAttribute("onclick", myFunc(this, array[i]);
btw. other solutions: when I used div.onclick
the onclick attr was not visible on div (result: <div></div>
), possibly I did something wrong.