blazerix blazerix - 1 year ago 94
CSS Question

Event handler not working for all elements

I'm trying to find a way to assign event handlers to each box that I create dynamically. At the moment the user can click "Add above" or "Add below" and 2 rows of boxes will appear wherever they clicked.

I'm also trying to make it so that when the user clicks on a specific square, a colorPicker will pop up and that specific square's color can be changed.

For some reason though, the colorpicker only pops up when the user clicks on the first two rows of squares. If more rows are dynamically added below or above the current set, nothing happens when you click on the squares.

Does anyone know why this might be happening?

What I have done/tried so far :

var theParent = document.querySelector(".container");
theParent.addEventListener("click", doSomething, false)
function doSomething(e) {
console.log("gets inside doSomething")
if ( !== e.currentTarget && !== "") {
var clickedItem =;
console.log("Clicked on " + clickedItem);
var led = document.getElementById(clickedItem)

console.log("new picker initialized")
picker = new Picker(led)
console.log("gets inside else case")
picker.settings.parent = led;

picker.on_done = function(colour) {


Answer Source

Try this on line 2:

$(document).on("click", ".container", doSomething);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download