user3896478
user3896478

Reputation: 41

Allow user to select the number of checkboxes equal to the value of a text input

you want help me, I have a form where I have an input text and 4 chechbox, what I do is, if you write a value in the input, is the same number of checked I can make the checkbox, eg write the value 3 in the input and in the group of checkbox only allows me to select any 3 boxes and missing fourth box is off or you can not do checked.

thanks and regards.

HTML:

    <input id="iddata_13561" name="data_13561" type="text" value="" >

    <input name="data" id="iddata_13562" type="checkbox"  value="1">
    <input name="data" id="iddata_13570" type="checkbox"  value="1">
    <input name="data" id="iddata_13578" type="checkbox"  value="1">
    <input name="data" id="iddata_13586" type="checkbox"  value="1">

JS:

     $(document).ready(function () {
           $("input[name='tech']").change(function () {

              var maxAllowed = $('#iddata_13561').attr('value');

              var cnt = $("input[name='tech']:checked").length;

             if (cnt > maxAllowed)
              {
                 $(this).prop("checked", "");
                 alert('Select maximum ' + maxAllowed + ' technologies!');
             }
          });
        });

Upvotes: 0

Views: 122

Answers (1)

James Montagne
James Montagne

Reputation: 78650

You don't want the attribute value, you want the current value. For that you use val:

var maxAllowed = $('#iddata_13561').val();

http://jsfiddle.net/efKAb/

Note: I changed name='data' to tech as I assume that was a copy/paste error.

Upvotes: 4

Related Questions