Reputation: 37
I am creating dynamic set of buttons
for (var i = 0; i < clr.length; ++i) {
button = document.createElement("button");
button.innerHTML = clr[i];
button.addEventListener('click', event);
button.style.backgroundColor = clr[i];
document.body.appendChild(button);
}
but I want the event to occur "only once" for each button. But I am unable to implement this correctly using javascript .
Upvotes: 0
Views: 172
Reputation: 56509
You can remove the attached event using removeEventListener once after it executed. By doing this the click event is handled only once as mentioned below,
function event(e) { //e represented the current element which is clicked
e.target.removeEventListener(e.type, arguments.callee);
alert('hi');
}
Upvotes: 1