dimanche 30 octobre 2016

Multiples checkboxes same .click function

Apologies for my lack of knowledge, I'm sure this is one of those simple fix solutions.

I have a number of checkboxes in my web page. When you check a check box a value of 1 is sent to a variable.

See example:

  $('#abilitytobenonjudgemental').click(function(){ 
    var check = document.getElementById("abilitytobenonjudgemental").checked
    if (check == true) {
    job2 += 1;
    job3 += 1;
    job7 += 1;
    workSkillsCheckboxCount += 1;  
    console.log(workSkillsCheckboxCount);   
   }
    else if (check == false) {
    job2 -= 1;
    job3 -= 1;
    job7 -= 1;
    workSkillsCheckboxCount -= 1; 
    console.log(workSkillsCheckboxCount);

  }
   });

A number of the checkboxes' functions double up and I would like to write a big function, rather than a number of functions. This function below doesn't seem to work.

   $('#teamwork, #goodcommunicator, #self-motivated, #computerskills,      #planning, #selfmanagement, #flexibleteamplayer, #timedeadlines').click(function()     { 
       var check = document.getElementById("teamwork, goodcommunicator,      planning, self-motivated, computerskills, selfmanagement, flexibleteamplayer, timedealines").checked
     if (check == true) {
     job1 += 1; 
     job2 += 1;
     job3 += 1;
     job4 += 1;
     job5 += 1;
     job6 += 1;
     job7 += 1;
     job8 += 1;
     workSkillsCheckboxCount += 1; 
     console.log(workSkillsCheckboxCount);
 }
    else if (check == false) {
     job1 -= 1; 
     job2 -= 1;
     job3 -= 1;
     job4 -= 1;
     job5 -= 1;
     job6 -= 1;
     job7 -= 1;
     job8 -= 1;
     workSkillsCheckboxCount -= 1; 
     console.log(workSkillsCheckboxCount);

 }
 }); 

I tried giving the appropriate checkboxes a class and then wrote this function. But that doesn't seem to be sending the values to the workSkillsCheckboxCount variable.

  $('.group1').click(function(){ 
        var check = document.getElementsByClassName("group1").checked
    if (check == true) {
    job1 += 1; 
    job2 += 1;
    job3 += 1;
    job4 += 1;
    job5 += 1;
    job6 += 1;
    job7 += 1;
    job8 += 1;
    workSkillsCheckboxCount += 1; 
    console.log(workSkillsCheckboxCount);
}
    else if (check == false) {
    job1 -= 1; 
    job2 -= 1;
    job3 -= 1;
    job4 -= 1;
    job5 -= 1;
    job6 -= 1;
    job7 -= 1;
    job8 -= 1;
    workSkillsCheckboxCount -= 1; 
    console.log(workSkillsCheckboxCount);

}
});




Aucun commentaire:

Enregistrer un commentaire