VueJS allow only 1 checkbox select based on class name












1














I have group of checkboxes:



<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>


I collecting checked values to data:



new Vue({
el: '#app',
data() {
return {
additional: ,
}
},
});


Can't figure out how to prevent checking more then 1 checkbox inside .group
I tried using radio, but then stranger things come up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?



Here is my pen: https://codepen.io/RomkaLTU/pen/LXOJgr?editors=1010










share|improve this question






















  • according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
    – Nguyễn Thanh Tú
    Nov 20 at 9:46












  • Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
    – RomkaLTU
    Nov 20 at 9:49












  • check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
    – Haseeb Rehman
    Nov 20 at 10:09
















1














I have group of checkboxes:



<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>


I collecting checked values to data:



new Vue({
el: '#app',
data() {
return {
additional: ,
}
},
});


Can't figure out how to prevent checking more then 1 checkbox inside .group
I tried using radio, but then stranger things come up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?



Here is my pen: https://codepen.io/RomkaLTU/pen/LXOJgr?editors=1010










share|improve this question






















  • according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
    – Nguyễn Thanh Tú
    Nov 20 at 9:46












  • Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
    – RomkaLTU
    Nov 20 at 9:49












  • check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
    – Haseeb Rehman
    Nov 20 at 10:09














1












1








1







I have group of checkboxes:



<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>


I collecting checked values to data:



new Vue({
el: '#app',
data() {
return {
additional: ,
}
},
});


Can't figure out how to prevent checking more then 1 checkbox inside .group
I tried using radio, but then stranger things come up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?



Here is my pen: https://codepen.io/RomkaLTU/pen/LXOJgr?editors=1010










share|improve this question













I have group of checkboxes:



<div class="additions">
<input type="checkbox" value="10" v-model="additional">
<input type="checkbox" value="30" v-model="additional">
<div class="group">
<input type="checkbox" value="50" v-model="additional">
<input type="checkbox" value="70" v-model="additional">
</div>
</div>


I collecting checked values to data:



new Vue({
el: '#app',
data() {
return {
additional: ,
}
},
});


Can't figure out how to prevent checking more then 1 checkbox inside .group
I tried using radio, but then stranger things come up, decided to stick with checkboxes. I could do it in jQuery or even vanilla JS I think, but I don't know where to put check (on change event method?). What is the best way to do it in VueJS?



Here is my pen: https://codepen.io/RomkaLTU/pen/LXOJgr?editors=1010







javascript vue.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 9:40









RomkaLTU

508718




508718












  • according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
    – Nguyễn Thanh Tú
    Nov 20 at 9:46












  • Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
    – RomkaLTU
    Nov 20 at 9:49












  • check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
    – Haseeb Rehman
    Nov 20 at 10:09


















  • according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
    – Nguyễn Thanh Tú
    Nov 20 at 9:46












  • Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
    – RomkaLTU
    Nov 20 at 9:49












  • check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
    – Haseeb Rehman
    Nov 20 at 10:09
















according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
– Nguyễn Thanh Tú
Nov 20 at 9:46






according to your codepen, I saw the lables Radio #1 and Radio #2, are they meant to be type="radio" rather than type="checkbox"?
– Nguyễn Thanh Tú
Nov 20 at 9:46














Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
– RomkaLTU
Nov 20 at 9:49






Sorry not radio, it was at the beginning but then I decided to stick with checkboxes. That was question about.
– RomkaLTU
Nov 20 at 9:49














check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
– Haseeb Rehman
Nov 20 at 10:09




check if this is what you are trying to achieve. codepen.io/Haeeb098/pen/eQebgz?editors=1011
– Haseeb Rehman
Nov 20 at 10:09












3 Answers
3






active

oldest

votes


















2














You can use different ways:



1. :disabled directive



<input type="checkbox" value="20" v-model="additional" :disabled="condition">



Using condition like additional.length > 0 you can disable checkbox if more then one already selected.



2. Watchers



data() {
...
},
watch: {
additional(newValue, oldValue) {
// new additional array value will be here every time any checkbox switched
}
}





share|improve this answer





























    1














    Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.



    <input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">

    computed: {
    hasAdditional() {
    return this.additional.length > 0
    }
    }


    Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional to return the sum of the number of additions, then use that to work if if you can select the group.



    Don’t rely on CSS classes. Use methods, watchers, and computed properties to work the logic out.






    share|improve this answer





















    • wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
      – Haseeb Rehman
      Nov 20 at 9:57










    • Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
      – Thomas Edwards
      Nov 20 at 9:59



















    0














    Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:



    <input type="checkbox" value="30" v-model="additional">
    <input type="checkbox" value="40" v-model="additional">
    <input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
    <input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">


    new Vue({
    el: '#app',
    data() {
    return {
    additional: ,
    additional_grouped: ,
    }
    },
    computed: {
    final: function(){
    return this.additional.concat(this.additional_grouped);
    }
    },
    methods: {
    uniqueCheck(e){
    this.additional_grouped = ;
    if (e.target.checked) {
    this.additional_grouped.push(e.target.value);
    }
    }
    }
    });





    share|improve this answer





















      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%2f53390091%2fvuejs-allow-only-1-checkbox-select-based-on-class-name%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use different ways:



      1. :disabled directive



      <input type="checkbox" value="20" v-model="additional" :disabled="condition">



      Using condition like additional.length > 0 you can disable checkbox if more then one already selected.



      2. Watchers



      data() {
      ...
      },
      watch: {
      additional(newValue, oldValue) {
      // new additional array value will be here every time any checkbox switched
      }
      }





      share|improve this answer


























        2














        You can use different ways:



        1. :disabled directive



        <input type="checkbox" value="20" v-model="additional" :disabled="condition">



        Using condition like additional.length > 0 you can disable checkbox if more then one already selected.



        2. Watchers



        data() {
        ...
        },
        watch: {
        additional(newValue, oldValue) {
        // new additional array value will be here every time any checkbox switched
        }
        }





        share|improve this answer
























          2












          2








          2






          You can use different ways:



          1. :disabled directive



          <input type="checkbox" value="20" v-model="additional" :disabled="condition">



          Using condition like additional.length > 0 you can disable checkbox if more then one already selected.



          2. Watchers



          data() {
          ...
          },
          watch: {
          additional(newValue, oldValue) {
          // new additional array value will be here every time any checkbox switched
          }
          }





          share|improve this answer












          You can use different ways:



          1. :disabled directive



          <input type="checkbox" value="20" v-model="additional" :disabled="condition">



          Using condition like additional.length > 0 you can disable checkbox if more then one already selected.



          2. Watchers



          data() {
          ...
          },
          watch: {
          additional(newValue, oldValue) {
          // new additional array value will be here every time any checkbox switched
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 10:06









          Xeelley

          314




          314

























              1














              Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.



              <input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">

              computed: {
              hasAdditional() {
              return this.additional.length > 0
              }
              }


              Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional to return the sum of the number of additions, then use that to work if if you can select the group.



              Don’t rely on CSS classes. Use methods, watchers, and computed properties to work the logic out.






              share|improve this answer





















              • wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
                – Haseeb Rehman
                Nov 20 at 9:57










              • Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
                – Thomas Edwards
                Nov 20 at 9:59
















              1














              Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.



              <input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">

              computed: {
              hasAdditional() {
              return this.additional.length > 0
              }
              }


              Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional to return the sum of the number of additions, then use that to work if if you can select the group.



              Don’t rely on CSS classes. Use methods, watchers, and computed properties to work the logic out.






              share|improve this answer





















              • wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
                – Haseeb Rehman
                Nov 20 at 9:57










              • Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
                – Thomas Edwards
                Nov 20 at 9:59














              1












              1








              1






              Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.



              <input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">

              computed: {
              hasAdditional() {
              return this.additional.length > 0
              }
              }


              Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional to return the sum of the number of additions, then use that to work if if you can select the group.



              Don’t rely on CSS classes. Use methods, watchers, and computed properties to work the logic out.






              share|improve this answer












              Don’t think about the DOM, don’t think about classes. Hard habit to break, I know.



              <input type="checkbox" value="50" v-model="additional" :disabled="hasAdditional">

              computed: {
              hasAdditional() {
              return this.additional.length > 0
              }
              }


              Use that as a starter for what you’re trying to do. Possibly you have to use a watcher to detect when it changes and uncheck ones that aren’t allowed. You could also change hasAdditional to return the sum of the number of additions, then use that to work if if you can select the group.



              Don’t rely on CSS classes. Use methods, watchers, and computed properties to work the logic out.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 at 9:51









              Thomas Edwards

              7,23331332




              7,23331332












              • wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
                – Haseeb Rehman
                Nov 20 at 9:57










              • Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
                – Thomas Edwards
                Nov 20 at 9:59


















              • wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
                – Haseeb Rehman
                Nov 20 at 9:57










              • Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
                – Thomas Edwards
                Nov 20 at 9:59
















              wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
              – Haseeb Rehman
              Nov 20 at 9:57




              wouldn't it disable the checkbox completely after selecting one and won't be able to select or deselect again?
              – Haseeb Rehman
              Nov 20 at 9:57












              Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
              – Thomas Edwards
              Nov 20 at 9:59




              Yes, that code isn’t complete, it’s a starter. I’m not completely sure of the functionality you’re trying to achieve so I can’t write the code for you. You could possibly have two data properties, and that if the first has more than 1 (as above), then the second set of checkboxes are disabled. Again, not sure what you’re trying to do.
              – Thomas Edwards
              Nov 20 at 9:59











              0














              Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:



              <input type="checkbox" value="30" v-model="additional">
              <input type="checkbox" value="40" v-model="additional">
              <input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
              <input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">


              new Vue({
              el: '#app',
              data() {
              return {
              additional: ,
              additional_grouped: ,
              }
              },
              computed: {
              final: function(){
              return this.additional.concat(this.additional_grouped);
              }
              },
              methods: {
              uniqueCheck(e){
              this.additional_grouped = ;
              if (e.target.checked) {
              this.additional_grouped.push(e.target.value);
              }
              }
              }
              });





              share|improve this answer


























                0














                Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:



                <input type="checkbox" value="30" v-model="additional">
                <input type="checkbox" value="40" v-model="additional">
                <input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
                <input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">


                new Vue({
                el: '#app',
                data() {
                return {
                additional: ,
                additional_grouped: ,
                }
                },
                computed: {
                final: function(){
                return this.additional.concat(this.additional_grouped);
                }
                },
                methods: {
                uniqueCheck(e){
                this.additional_grouped = ;
                if (e.target.checked) {
                this.additional_grouped.push(e.target.value);
                }
                }
                }
                });





                share|improve this answer
























                  0












                  0








                  0






                  Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:



                  <input type="checkbox" value="30" v-model="additional">
                  <input type="checkbox" value="40" v-model="additional">
                  <input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
                  <input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">


                  new Vue({
                  el: '#app',
                  data() {
                  return {
                  additional: ,
                  additional_grouped: ,
                  }
                  },
                  computed: {
                  final: function(){
                  return this.additional.concat(this.additional_grouped);
                  }
                  },
                  methods: {
                  uniqueCheck(e){
                  this.additional_grouped = ;
                  if (e.target.checked) {
                  this.additional_grouped.push(e.target.value);
                  }
                  }
                  }
                  });





                  share|improve this answer












                  Thanks for pointing me out, but I choosed solution without disabling input as it get's very confusing for the end user. What I did:



                  <input type="checkbox" value="30" v-model="additional">
                  <input type="checkbox" value="40" v-model="additional">
                  <input type="checkbox" value="10" v-model="additional_grouped" @change="uniqueCheck">
                  <input type="checkbox" value="20" v-model="additional_grouped" @change="uniqueCheck">


                  new Vue({
                  el: '#app',
                  data() {
                  return {
                  additional: ,
                  additional_grouped: ,
                  }
                  },
                  computed: {
                  final: function(){
                  return this.additional.concat(this.additional_grouped);
                  }
                  },
                  methods: {
                  uniqueCheck(e){
                  this.additional_grouped = ;
                  if (e.target.checked) {
                  this.additional_grouped.push(e.target.value);
                  }
                  }
                  }
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 12:12









                  RomkaLTU

                  508718




                  508718






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53390091%2fvuejs-allow-only-1-checkbox-select-based-on-class-name%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

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno