-3

I want to limit checked checkboxes to 3, I couldn't do it if you can help that'd be a great favour.

        <div class="col-md-4" dir="ltr" ><center>
<span class="button-checkbox">
    <button type="button" style="width:200px;text-align: left;" class="btn" data-color="primary">'.$icon_image.' '.$group.' </button>
    <input type="checkbox" name="icon[]" value='.$sgid.'  />
</span>

I did try to write something here it is, also the checkboxes are echoed by php so it's depending on the mysql DB data.

var limit = 3;
$('input.icon[]').on('change', function(evt) {
   if($(this).siblings(':checked').length >= limit) {
       this.checked = false;
   }
});
MAH3R
  • 27
  • 6
  • 2
    1. I only see **one** checkbox in your example. 2. Where is your javascript code? Did you try to write something yourself? – Dekel Dec 25 '16 at 22:11
  • 1
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). From that second link: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." – Timothy Truckle Dec 25 '16 at 22:11
  • Hello, I did try to write something here it is – MAH3R Dec 25 '16 at 22:13
  • 1
    Don't add it in the comments. Update your question and put it there. – Dekel Dec 25 '16 at 22:13
  • I have updated the question – MAH3R Dec 25 '16 at 22:16

1 Answers1

1

Your javascript function would be like:

var limit = 3;
$('input[type=checkbox]').on('change', function(evt) {
   if($(this).siblings(':checked').length >= limit) {
       this.checked = false;
   }
});
Hicaro
  • 687
  • 2
  • 8
  • 21
  • Are you adding this inside `$(document).ready(function(){...});`? – Hicaro Dec 25 '16 at 22:35
  • Oh no I just used a – MAH3R Dec 25 '16 at 22:44
  • I would start by that. It only fires you js code when your DOM is ready. Take a look at this to learn more about [jQuery on ready](http://stackoverflow.com/questions/13062246/when-should-i-use-jquerys-document-ready-function) – Hicaro Dec 25 '16 at 22:48