Link to home
Start Free TrialLog in
Avatar of andreni78
andreni78

asked on

Limit the number of check boxes selected



I have a form with 5 checkboxes (could be more) along with other form elements, can you give me a function that constantly (on keypress?) check the number of check boxes being selected and limit them to 3? If a user select more than 3 (the 4th one) then it will pop a message "Please select only 3 options"

Thanks,

Andre

Part of my form code:


--------------------------------

<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="1">A</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="2">B</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="3">C</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="4">D</td>
</tr>
<tr>                                    
      <td>&nbsp;</td>
      <td><input type="checkbox" name="SceneType" value="5">E</td>
</tr>

--------------------------------
SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dang, someone beat me to it..
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sajuks
sajuks

:-) :-) :-).
' ' ' ' ' ' ' ' ' ' '  ' '
' ' ' ' ' ' ' ' ' ' '  '
Its raining solutions
Its raining confusions :-)

Vinny, select four checkboxes, unselect all again and check again one.

Thanks Zvonko,

That will teach to me to code before I finish my first mug of espresso :D


<script type='text/javascript'>
var chkd = 0;
function checkit(chObj)
{
 
  chkd += (chObj.checked) ? 1 : -1;
  if (chkd > 3)
  {
    alert("More then three boxes checked.");
  chObj.checked = false;
    chkd--;
  }
}
</script>
</head>
<body>
<form>
<table>
<tr>                              
    <td>&nbsp;</td>
    <td><input type="checkbox" name="SceneType" value="1" onClick="checkit(this)" >A</td>
</tr>

Vinny
Ok, but now you can have only ONE group of checkboxes to check for max.
My example can check many groups by checkbox name.

Hi Zvonko,

 I simply threw in the solution to throw it into the ring as another alternative.  I personally wouldn't code it that way for myself -- it uses a global variable (chkd); something I try to stay away.

 Besides I'm still sipping my espresso :D

Vinny
Avatar of andreni78

ASKER

Thank you all for your help. I choose Zvonko solution because it is elegant, simple, flexible and generic.

Andre
Thanks for points and thank you for the feedback.
Splitting points is always the best approach when several experts helped you to find a solution.

Cheers,
Zvonko
Thanks for following up andreni78, and thanks for giving consideration (points) to the rest of us!

Take care,
Kenman