1

I have a series of Checkboxes:

<tr id="tr5" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td5"><input  name="benefit" value="Bonuses" id="benefit5" type="checkbox" onchange='addition();'</td>
<td class="td5"><label for="benefit5"> <b>Bonuses</b></label></td>

<tr id="tr6" onmouseover="changeBackgroundColor(this.id)" onmouseout="changeBackgroundColor2(this.id)">
<td class="td6"><input  name="benefit" value="Final salary pension" id="benefit6" type="checkbox" onchange='addition();'</td>
<td class="td6"><label for="benefit6"> <b>Final salary pension</b></label></td>

Once a user has selected 3 checkboxes, is it possible to disable the rest in one hit (there are 30 checkboxes - I could do it individually but that seems a pain)? Is so, how would one go about doing that? Also, if the user then un-selected one of the check boxes, is it possible to enable them again?

EDIT: If possible - could someone point me in the right direction, code wise please?

Thanks in advance,

H.

3
  • @mplungjan - any chance of being able to point me in the right direction code wise? Commented Feb 18, 2013 at 9:55
  • I'm working on it - let's see if I make it ;) Commented Feb 18, 2013 at 9:56
  • @mplungjan really appreciated - struggling like hell here!!! Commented Feb 18, 2013 at 10:04

1 Answer 1

2

DEMO

var chk=0;
function checkCheckboxes() {
  var checkboxes = document.getElementsByName("benefit");
  for (var i=0;i<checkboxes.length;i++) {
    chk += checkboxes[i].checked?1:0; // count in case we reload
    checkboxes[i].onclick=function() { // set up event handler for each
      chk+=this.checked?1:-1; // add or subtract one
      if (chk > 3) {
        console.log(chk,"too many") 
        this.checked=false;
        chk--; // we counted too many
      }
    }
  }
}


function changeBackgroundColor(row,on) {
  var id = row.id; // if you need that
  row.style.backgroundColor=(on)?"red":"white";
}
window.onload=function() {
    var trs = document.getElementById("table1").rows;
    for (var i=0;i<trs.length;i++) {
      trs[i].onmouseover=function() {
        changeBackgroundColor(this,1);
      }
      trs[i].onmouseout=function() {
        changeBackgroundColor(this,0);
      }
    }
    checkCheckboxes();
}

using

<table id="table1">
<tr id="tr1">
<td class="td1"><input  name="benefit" value="Bonuses" id="benefit1" type="checkbox"</td>
<td class="td1"><label for="benefit1"> <b>Bonuses</b></label></td>
</tr>
    <tr id="tr2">
<td class="td2"><input  name="benefit" value="Bonuses" id="benefit2" type="checkbox"</td>
<td class="td2"><label for="benefit2"> <b>Bonuses</b></label></td>
</tr>
    <tr id="tr3">
<td class="td3"><input  name="benefit" value="Bonuses" id="benefit3" type="checkbox"</td>
<td class="td3"><label for="benefit3"> <b>Bonuses</b></label></td>
</tr>
    <tr id="tr4">
<td class="td4"><input  name="benefit" value="Bonuses" id="benefit4" type="checkbox"</td>
<td class="td4"><label for="benefit4"> <b>Bonuses</b></label></td>
</tr>
        </table>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.