too many checked












0















The mind boggles - my colleague sent me this script - I am sure I overlook something simple



http://jsfiddle.net/mplungjan/j4m62/



which for some reason I cannot figure out, checks all three checkboxes regardless of which I click - I would expect only the first two to be checked on click of either of the first two and the third to be checked on its own



HTML:



<input class="check-all" name="checkbox"  id="checkbox1" type="checkbox" value="on"/><label for="checkbox1">Check 1&2</label><br/>

<input class="check-allfeatured" name="checkbox" id="checkbox2" type="checkbox" value="on"/><label for="checkbox2">Check 1&2</label><br/>

<input class="check-alldel" name="deleteids" id="deleteids1" type="checkbox" value="on"/><label for="deleteids1">Check this only</label>


JavaScript:



$(document).ready(function(){
$('.check-all:checkbox').click(function(event) {
var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
console.log(group+':'+event.target.checked);
$(group).each(function(){
$(this).attr("checked",event.target.checked);
});
});

$('.check-alldel:checkbox').click(function(event) {
var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
console.log(group+':'+event.target.checked);
$(group).each(function(){
$(this).attr("checked",event.target.checked);
});
});

$('.check-allfeatured:checkbox').click(function(event) {
var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
console.log(group+':'+event.target.checked);
$(group).each(function(){
$(this).attr("checked",event.target.checked);
});
});
});









share|improve this question





























    0















    The mind boggles - my colleague sent me this script - I am sure I overlook something simple



    http://jsfiddle.net/mplungjan/j4m62/



    which for some reason I cannot figure out, checks all three checkboxes regardless of which I click - I would expect only the first two to be checked on click of either of the first two and the third to be checked on its own



    HTML:



    <input class="check-all" name="checkbox"  id="checkbox1" type="checkbox" value="on"/><label for="checkbox1">Check 1&2</label><br/>

    <input class="check-allfeatured" name="checkbox" id="checkbox2" type="checkbox" value="on"/><label for="checkbox2">Check 1&2</label><br/>

    <input class="check-alldel" name="deleteids" id="deleteids1" type="checkbox" value="on"/><label for="deleteids1">Check this only</label>


    JavaScript:



    $(document).ready(function(){
    $('.check-all:checkbox').click(function(event) {
    var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
    console.log(group+':'+event.target.checked);
    $(group).each(function(){
    $(this).attr("checked",event.target.checked);
    });
    });

    $('.check-alldel:checkbox').click(function(event) {
    var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
    console.log(group+':'+event.target.checked);
    $(group).each(function(){
    $(this).attr("checked",event.target.checked);
    });
    });

    $('.check-allfeatured:checkbox').click(function(event) {
    var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
    console.log(group+':'+event.target.checked);
    $(group).each(function(){
    $(this).attr("checked",event.target.checked);
    });
    });
    });









    share|improve this question



























      0












      0








      0








      The mind boggles - my colleague sent me this script - I am sure I overlook something simple



      http://jsfiddle.net/mplungjan/j4m62/



      which for some reason I cannot figure out, checks all three checkboxes regardless of which I click - I would expect only the first two to be checked on click of either of the first two and the third to be checked on its own



      HTML:



      <input class="check-all" name="checkbox"  id="checkbox1" type="checkbox" value="on"/><label for="checkbox1">Check 1&2</label><br/>

      <input class="check-allfeatured" name="checkbox" id="checkbox2" type="checkbox" value="on"/><label for="checkbox2">Check 1&2</label><br/>

      <input class="check-alldel" name="deleteids" id="deleteids1" type="checkbox" value="on"/><label for="deleteids1">Check this only</label>


      JavaScript:



      $(document).ready(function(){
      $('.check-all:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });

      $('.check-alldel:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });

      $('.check-allfeatured:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });
      });









      share|improve this question
















      The mind boggles - my colleague sent me this script - I am sure I overlook something simple



      http://jsfiddle.net/mplungjan/j4m62/



      which for some reason I cannot figure out, checks all three checkboxes regardless of which I click - I would expect only the first two to be checked on click of either of the first two and the third to be checked on its own



      HTML:



      <input class="check-all" name="checkbox"  id="checkbox1" type="checkbox" value="on"/><label for="checkbox1">Check 1&2</label><br/>

      <input class="check-allfeatured" name="checkbox" id="checkbox2" type="checkbox" value="on"/><label for="checkbox2">Check 1&2</label><br/>

      <input class="check-alldel" name="deleteids" id="deleteids1" type="checkbox" value="on"/><label for="deleteids1">Check this only</label>


      JavaScript:



      $(document).ready(function(){
      $('.check-all:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });

      $('.check-alldel:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });

      $('.check-allfeatured:checkbox').click(function(event) {
      var group = 'input:checkbox[name=' + $(this).attr('name') + ']';
      console.log(group+':'+event.target.checked);
      $(group).each(function(){
      $(this).attr("checked",event.target.checked);
      });
      });
      });






      jquery checkbox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 17 '12 at 12:52









      T.J. Crowder

      683k12112121306




      683k12112121306










      asked Apr 17 '12 at 12:47









      mplungjanmplungjan

      87.7k21124181




      87.7k21124181
























          2 Answers
          2






          active

          oldest

          votes


















          1














          While not pretty, the corrected version is here: http://jsfiddle.net/j4m62/8/



          Your problem was that you were not putting quotes around the names of your checkbox name assertion.



          Change:



          var group = 'input:checkbox[name=' + $(this).attr('name') + ']';


          into:



          var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';


          and you will get the behavior you are expecting. But you really have a lot of duplicated code so you should probably refactor since you are performing the same operation regardless of the checkbox clicked. Check this out.



          Thanks @limelights for pointing out that I forgot to explicitly pass event in. Updated link.






          share|improve this answer


























          • Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

            – Henrik Andersson
            Apr 17 '12 at 13:02











          • Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

            – veeTrain
            Apr 17 '12 at 13:06











          • Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

            – mplungjan
            Apr 17 '12 at 13:12













          • @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

            – veeTrain
            Apr 17 '12 at 13:35



















          1














          The code below will check all checkboxes because the group variable contains input:checkbox[name=checkbox] and that will select all checkboxes in your case.



          $(group).each(function() {
          $(this).attr("checked", event.target.checked);
          });





          share|improve this answer


























          • Thanks! - accepted the other since it re-factored the code too

            – mplungjan
            Apr 17 '12 at 13:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10191541%2ftoo-many-checked%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          While not pretty, the corrected version is here: http://jsfiddle.net/j4m62/8/



          Your problem was that you were not putting quotes around the names of your checkbox name assertion.



          Change:



          var group = 'input:checkbox[name=' + $(this).attr('name') + ']';


          into:



          var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';


          and you will get the behavior you are expecting. But you really have a lot of duplicated code so you should probably refactor since you are performing the same operation regardless of the checkbox clicked. Check this out.



          Thanks @limelights for pointing out that I forgot to explicitly pass event in. Updated link.






          share|improve this answer


























          • Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

            – Henrik Andersson
            Apr 17 '12 at 13:02











          • Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

            – veeTrain
            Apr 17 '12 at 13:06











          • Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

            – mplungjan
            Apr 17 '12 at 13:12













          • @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

            – veeTrain
            Apr 17 '12 at 13:35
















          1














          While not pretty, the corrected version is here: http://jsfiddle.net/j4m62/8/



          Your problem was that you were not putting quotes around the names of your checkbox name assertion.



          Change:



          var group = 'input:checkbox[name=' + $(this).attr('name') + ']';


          into:



          var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';


          and you will get the behavior you are expecting. But you really have a lot of duplicated code so you should probably refactor since you are performing the same operation regardless of the checkbox clicked. Check this out.



          Thanks @limelights for pointing out that I forgot to explicitly pass event in. Updated link.






          share|improve this answer


























          • Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

            – Henrik Andersson
            Apr 17 '12 at 13:02











          • Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

            – veeTrain
            Apr 17 '12 at 13:06











          • Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

            – mplungjan
            Apr 17 '12 at 13:12













          • @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

            – veeTrain
            Apr 17 '12 at 13:35














          1












          1








          1







          While not pretty, the corrected version is here: http://jsfiddle.net/j4m62/8/



          Your problem was that you were not putting quotes around the names of your checkbox name assertion.



          Change:



          var group = 'input:checkbox[name=' + $(this).attr('name') + ']';


          into:



          var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';


          and you will get the behavior you are expecting. But you really have a lot of duplicated code so you should probably refactor since you are performing the same operation regardless of the checkbox clicked. Check this out.



          Thanks @limelights for pointing out that I forgot to explicitly pass event in. Updated link.






          share|improve this answer















          While not pretty, the corrected version is here: http://jsfiddle.net/j4m62/8/



          Your problem was that you were not putting quotes around the names of your checkbox name assertion.



          Change:



          var group = 'input:checkbox[name=' + $(this).attr('name') + ']';


          into:



          var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';


          and you will get the behavior you are expecting. But you really have a lot of duplicated code so you should probably refactor since you are performing the same operation regardless of the checkbox clicked. Check this out.



          Thanks @limelights for pointing out that I forgot to explicitly pass event in. Updated link.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 17 '12 at 13:05

























          answered Apr 17 '12 at 12:56









          veeTrainveeTrain

          2,43611838




          2,43611838













          • Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

            – Henrik Andersson
            Apr 17 '12 at 13:02











          • Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

            – veeTrain
            Apr 17 '12 at 13:06











          • Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

            – mplungjan
            Apr 17 '12 at 13:12













          • @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

            – veeTrain
            Apr 17 '12 at 13:35



















          • Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

            – Henrik Andersson
            Apr 17 '12 at 13:02











          • Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

            – veeTrain
            Apr 17 '12 at 13:06











          • Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

            – mplungjan
            Apr 17 '12 at 13:12













          • @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

            – veeTrain
            Apr 17 '12 at 13:35

















          Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

          – Henrik Andersson
          Apr 17 '12 at 13:02





          Your fiddle isn't correct unfortunately, you've forgotten to include the event. Fixed version jsfiddle.net/j4m62/11

          – Henrik Andersson
          Apr 17 '12 at 13:02













          Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

          – veeTrain
          Apr 17 '12 at 13:06





          Thanks @limelights! I have updated the link with your addition. Great find. I guess 'event' floated in since I didn't see an error using Chrome and it 'worked'.

          – veeTrain
          Apr 17 '12 at 13:06













          Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

          – mplungjan
          Apr 17 '12 at 13:12







          Drat - I had already suspected that, but though nah... :) - PS not my code to begin with btw :|

          – mplungjan
          Apr 17 '12 at 13:12















          @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

          – veeTrain
          Apr 17 '12 at 13:35





          @mplungjan; no problem. Be sure to encourage your colleague with refactoring; glad we could clear up the confusion.

          – veeTrain
          Apr 17 '12 at 13:35













          1














          The code below will check all checkboxes because the group variable contains input:checkbox[name=checkbox] and that will select all checkboxes in your case.



          $(group).each(function() {
          $(this).attr("checked", event.target.checked);
          });





          share|improve this answer


























          • Thanks! - accepted the other since it re-factored the code too

            – mplungjan
            Apr 17 '12 at 13:16
















          1














          The code below will check all checkboxes because the group variable contains input:checkbox[name=checkbox] and that will select all checkboxes in your case.



          $(group).each(function() {
          $(this).attr("checked", event.target.checked);
          });





          share|improve this answer


























          • Thanks! - accepted the other since it re-factored the code too

            – mplungjan
            Apr 17 '12 at 13:16














          1












          1








          1







          The code below will check all checkboxes because the group variable contains input:checkbox[name=checkbox] and that will select all checkboxes in your case.



          $(group).each(function() {
          $(this).attr("checked", event.target.checked);
          });





          share|improve this answer















          The code below will check all checkboxes because the group variable contains input:checkbox[name=checkbox] and that will select all checkboxes in your case.



          $(group).each(function() {
          $(this).attr("checked", event.target.checked);
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 9:09

























          answered Apr 17 '12 at 12:52









          CyclonecodeCyclonecode

          21.2k95172




          21.2k95172













          • Thanks! - accepted the other since it re-factored the code too

            – mplungjan
            Apr 17 '12 at 13:16



















          • Thanks! - accepted the other since it re-factored the code too

            – mplungjan
            Apr 17 '12 at 13:16

















          Thanks! - accepted the other since it re-factored the code too

          – mplungjan
          Apr 17 '12 at 13:16





          Thanks! - accepted the other since it re-factored the code too

          – mplungjan
          Apr 17 '12 at 13:16


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10191541%2ftoo-many-checked%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ