vendredi 19 juin 2015

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

I am trying to dynamically create a set of check-boxes that each call a function differently when clicked

function initAllButtons(variable, length)
{
   for(c = 0; c < length; c++)
   {
      clicks.push(true);
   }


   for(i = 0; i < 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 is just an array of strings. I really want to call the UpdateData function (which I have tested and works like it should) with the value or index of the button pressed, but nothing seems to be working.

This version just calls updateData ten times with index = 10. It obviously is not looking at the buttons as individual things. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire