mercredi 14 juin 2017

jquery checkbox listener for loop

i have this javascript file:

http://ift.tt/2sBtLNV

and am trying to make it so that the checkboxes created by the bodyView, have click listeners, however right now i can only create a listener for one checkbox at a time, is there any way to improve this?

Here is the relevant part of the code:

var bodyView = {

  init: function() {
    this.render();
  },

  render: function() {
    var student, i, x;

    var schoolDays = octopus.getSchoolDays();
    var students = octopus.getStudents();


    for(i = 0;i < students.length; i++) {
      octopus.setCurrentStudent(students[i]);
      var daysMissed = octopus.getDaysMissed();
      $('#students').append('<tr class="'+i+'"><td class="name-col">' + students[i].name + '</td></tr>');
      for(x = 1;x < schoolDays; x++) {
        $('.'+i).append('<td id="check'+i+'" class="attend-col"><input type="checkbox"></td>');
      };
      $('.'+i).append('<td class="missed-col">'+ daysMissed + '</td>');
    };
    $(function(){
      $('#check0').click(function() {
        octopus.setCurrentStudent(students[0]);
        if($(this).is(':checked')){
          octopus.incrementDaysMissed();
        } else if(!$(this).is(':checked')){
          octopus.decreaseDaysMissed();
        }
      })
    })
  }
}




Aucun commentaire:

Enregistrer un commentaire