0

I have a loop that create form fields (input) dynamically via a loop. I set the ID as a property within the loop so each item has a different ID (I need to attach different parameters for each item).

I keep getting the error ...not a function which shows the element is not created when my click event is executed... even though my onclick event is placed after the .appendChild method?

Is there something I am not getting?

// Iterate through array to create form fields for filter
             for (var i = 0; i < filterUnique.length; i++) {
                   var input = document.createElement('input');
                    $(input).attr({
                      "type": "Button",
                      "id": "filterType" + i,
                      "value": filterUnique[i]
                    });
                    document.getElementById("input_filter").appendChild(input);
                    document.getElementById("filterType"+i).onclick(console.log("test"));
             };
Grem357
  • 92
  • 1
  • 10
  • Marked as a duplicate... but It doesn't seem to be? Not tot he one linked anyway.. I need the onclick within the loop as I want to add it to each element I create, isn't it in the right place then? – Grem357 Nov 13 '17 at 10:19
  • You can try onclick() event – Satish Nov 13 '17 at 10:22

0 Answers0