javascript - How to call unique function for dynamically created checkboxes in a div -


i trying dynamically create set of check-boxes each call function differently when clicked

function initallbuttons(variable, length) {    for(c = 0; c < length; c++)    {       clicks.push(true);    }      for(i = 0; < length; ++i)    {       var label = document.createelement("label");       var checkbox = document.createelement("input");       checkbox.type = "checkbox";       checkbox.checked = true;       checkbox.value = btns[i];       checkbox.class = "colorcoder";       label.appendchild(checkbox);        document.getelementbyid('buttons').appendchild(checkbox);        $('#buttons').on('click', function(){updatedata(i,clicks);});    } } 

btns array of strings. want call updatedata function (which have tested , works should) value or index of button pressed, nothing seems working.

this version calls updatedata ten times index = 10. not looking @ buttons individual things. doing wrong?

when dynamically adding elements dom, attaching event listeners gets tricky. perfect use case delegate on parent container:

        var clickcontainer = document.getelementbyid('clickcontainer');          clickcontainer.addeventlistener('click', function(e) {             var target = e.target || e.srcelement;             e.stoppropagation();         }, false); 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -