title: jquery to disable checkbox once the selected item is 5
body: <p>How would i be able to disable the rest of the checkboxes once the selected items is 5? I was thinking there could be a way to do this in jquery. Here is my code below.</p>
  
  <pre><code>&lt;table&gt;
  @{ var i = 0;}
  @foreach (var testimonials in Model.Testimonials)
  {
  &lt;tr&gt;
      &lt;td style="padding: 2px 0 2px 2px;"&gt;
          @Html.CheckBox("Testimonials[" + i.ToString() + "].DisplayTestimonials", testimonials.DisplayTestimonials.Value, new { @class = "chkItems" })
          @Html.Hidden("Testimonials[" + i.ToString() + "].ResponseId", testimonials.ResponseId.ToString())
      &lt;/td&gt;
      &lt;td style="padding: 2px 0 2px 2px;"&gt;@Html.TextBox("Testimonials[" + i.ToString() + "].FirstName", testimonials.FirstName, new { @readonly = "readonly", @class = "TextBoxAsLabel" })&lt;/td&gt;
      &lt;td style="padding: 2px 0 2px 2px;"&gt;@Html.TextBox("Testimonials[" + i.ToString() + "].LastName", testimonials.LastName, new { @readonly = "readonly", @class = "TextBoxAsLabel" })&lt;/td&gt;
      &lt;td style="padding: 2px 0 2px 2px;"&gt;@Html.TextBox("Testimonials[" + i.ToString() + "].Question5Answer", testimonials.Question5Answer.ToString(), new { @readonly = "readonly", @class = "TextBoxAsLabel" })&lt;/td&gt;
  &lt;/tr&gt;
                                     i++;
  }
  </code></pre>
  
  <p></p>
  
  <p>Solution:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function () {
      function countchecked() {
          var n = $("input:checked").length;
          if (n &gt;= 5)
              $('.chkItems:not(:checked)').attr("disabled", "disabled");
          else
              $('.chkItems:not(:checked)').removeAttr("disabled");
      }
      $(":checkbox").click(countchecked);
  });
  </code></pre>
  
  <p></p>