Advertisement

09.10.2008 at 08:05AM PDT, ID: 23719483 | Points: 500
[x]
Attachment Details

Javascript checked toggle boxes not working capturing unchecked

Asked by webkiwi1 in JScript, JavaScript, Active Server Pages (ASP)

Hello experts,

Javascript, ASP

I am using the code below to capture values on an ASP page when the user checks the checkbox. The user can only select three boxes.

The problem I am having is the code does not allow for the user unchecking a box, and selecting another one. i.e. 1, 2, 3, unselect 2, select 4 = 3 checked boxes.

I need to add a piece that will capture the unselect and remove the value from the string that is being built.

Hope this makes sense.

I do not know enough about java to know where or how to add the code.
. Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
<script type="text/javascript">
var checkedBoxes=0; 
var thebox;
var checked=null;
function toggleBoxes(thebox) {
    if((thebox.checked==true) && (checkedBoxes+countUserSel()<3)) {
        checkedBoxes=checkedBoxes+1;
//        debugger 
        document.getElementById("checkedRows").value=document.getElementById("checkedRows").value + "," + thebox.value;
        return true;
    }
    else if((thebox.checked==true) && (checkedBoxes+countUserSel()==3)) {
        alert("You can only select 3 properties to compare!");
        return false;
    }
    else{ 
        var selVals=document.getElementById("checkedRows").value ;
        selVals=selVals.replace(thebox.value,'');
        document.getElementById("checkedRows").value=selVals;
        checkedBoxes=checkedBoxes-1;
        return true;
    }
}
 
function setHref(btn) {
    //alert(document.getElementById("checkedRows").value)
         var vals=document.getElementById("checkedRows").value.split(",");
    //for(i=0;i<3;i++) alert(vals[i])      
    var qstr="asmt6_newgrid.asp?pin=<%=wpin%>";
    btn.href=""; 
    var usrsel=new Array(2), i, count=countUserSel();
    
      //alert(count);
    if (count+checkedBoxes>3){
        alert("You have selected "+(count+checkedBoxes)+ " values.  Please change your selection so that there are three or less.")
        return false;
    }
        
    count=0
      for (i=0;i<3;i++) {
          //debugger
          usrsel[i]=document.getElementById("user"+i).value;
          if (usrsel[i].length>0){
              count++;
              qstr= qstr + "&cmp" + count + "pin=" + usrsel[i];
          }
          
              
      }
      
      i=0;
    while(count<=3) {
        count++;
        i++;
        if (vals[i]) 
            qstr= qstr + "&cmp" + count + "pin=" + vals[i];
    }
    btn.href=btn.href+qstr;
   return true;
}
 
function countUserSel(){
    var usrsel=new Array(2), i, count=0;
    for (i=0;i<3;i++){
        usrsel[i]=document.getElementById("user"+i).value;
            if (usrsel[i].length > 0)
                  count++;
    }
    return count;
 
}
</script>
[+][-]09.10.2008 at 10:05AM PDT, ID: 22440478

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 10:07AM PDT, ID: 22440502

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 10:09AM PDT, ID: 22440525

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 12:44PM PDT, ID: 22442184

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.10.2008 at 01:47PM PDT, ID: 22442839

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.11.2008 at 07:11AM PDT, ID: 22450061

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.24.2008 at 11:19PM PDT, ID: 22801893

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628