• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

Checkbox onclick issue

Hello,

I have 3 checkboxes on my page and I am calling the below mentioned function.

////////////////////////////////////////////////
function contactTypeFilterChanged()
////////////////////////////////////////////////
{

    with (document.all.contactHistoryGrid)
      {
          //alert(document.all.chkmail.value=1)
          
          if((document.all.chkmail.checked==true)&&(document.all.chkmail.checked==true)&&(document.all.chkorgnotes.checked==true))
          fixedFilterPart = "(contact_type in('M@MSG','Phone','Fax','Letter'))"
          
          if((document.all.chkmail.checked==true)&&(document.all.chkorgnotes.checked==true))
          fixedFilterPart = "(contact_type in('M@MSG','Phone','Fax','Letter'))"
          if((document.all.chkmail.checked==true)&&(document.all.chkconnotes.checked==true))
          fixedFilterPart = "(contact_type in('M@MSG','Phone','Fax','Letter'))"
          
          if(document.all.chkmail.checked==true)
            fixedFilterPart = "(contact_type='M@MSG')"
            
          if(document.all.chkconnotes.checked==true)
          fixedFilterPart = "contact_type in('Phone','Fax','Letter')"
      
            if(document.all.chkorgnotes.checked==true)
            fixedFilterPart = "contact_type in('Phone','Fax','Letter')"
            
            
            alert(fixedFilterPart);
            
            
                  
            
      }
      
   
            
      
}


Say for example if I click the first checkbox it will go to this condition
 if(document.all.chkmail.checked==true)
            fixedFilterPart = "(contact_type='M@MSG')"


And if I clcik on the second checkbox i.e now both first and second checkboxes are checked it is going to
if(document.all.chkconnotes.checked==true)
          fixedFilterPart = "contact_type in('Phone','Fax','Letter')"
instead of
 if((document.all.chkmail.checked==true)&&(document.all.chkorgnotes.checked==true))
          fixedFilterPart = "(contact_type in('M@MSG','Phone','Fax','Letter'))"
0
sureshraina
Asked:
sureshraina
  • 2
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
Several problems with your code - please show the html and tell us what you want to have happen since it looks like you are repeating yourself over and over

1. why use "with" and then not use it
2. why use .all. when it is IE only and you can use document.contactHistoryGrid and document.contactHistoryGrid.chkconnotes
3. you need to read about "else"

  if((document.all.chkmail.checked==true)&&(document.all.chkmail.checked==true)&&(document.all.chkorgnotes.checked==true)) {
    fixedFilterPart = "(contact_type in('M@MSG','Phone','Fax','Letter'))"
  }
  else if((document.all.chkmail.checked==true)&&(document.all.chkorgnotes.checked==true)) {
    ...
  }


0
 
sureshrainaAuthor Commented:
Thanks for your suggestions. Its working fine now.
0
 
Pravin AsarCommented:
Here is example, how you could streamline the assigning variable value based on check boxes selection.

<script language="javascript">
function chkBoxProc (fld) {
      var val1 = fld.form.chk1.checked;
      var val2 = fld.form.chk2.checked;
      var val3 = fld.form.chk3.checked;
      var msg = 'None checked';
      if (val1 && val2 && val3) {
           msg = 'Checked 1, 2 and 3';
      }
      else if (val1 && val2) {
           msg = 'Checked 1, 2 ';
      }
      else if (val1 && val3) {
           msg = 'Checked 1, 3 ';
      }
      else if (val2 && val3) {
           msg = 'Checked 2, 3 ';
      }
      else if (val1 ) {
           msg = 'Checked 1';
      }
      else if (val2 ) {
           msg = 'Checked 2';
      }
      else if (val3 ) {
           msg = 'Checked 3';
      }
      document.getElementById ('msg').innerHTML = msg;

}
</script>

<form name="form1">
<input type="checkbox" name="chk1" value="1" onclick="chkBoxProc(this);">Check Option 1
<input type="checkbox" name="chk2" value="2" onclick="chkBoxProc(this);">Check Option 2
<input type="checkbox" name="chk3" value="3"onclick="chkBoxProc(this);">Check Option 3
</form>
<div id="msg"></div>
0
 
Michel PlungjanIT ExpertCommented:
:)

<script>
var flds = "chk1,chk2,chk3".split(",")
var vals = new Array(2,4,8)
var msgs = new Array();
msgs[0]  = 'None checked';
msgs[2]  = '1 checked';
msgs[4]  = '2 checked';
msgs[6]  = '1 & 2 checked';
msgs[8]  = '3 checked';
msgs[10]  = '1 & 3 checked';
msgs[12]  = '2 & 3 checked';
msgs[14]  = '1, 2 & 3 checked';


function chkBoxProc (fld) {
  var val = 0;
  for (i=0;i<flds.length;i++) {
    val += (fld.form.elements[flds[i]].checked)?vals[i]:0;
  }
  document.getElementById ('msg').innerHTML = msgs[val];

}
</script>
<form>
<input type="checkbox" name="chk1" onClick="chkBoxProc(this)">1
<input type="checkbox" name="chk2" onClick="chkBoxProc(this)">2
<input type="checkbox" name="chk3" onClick="chkBoxProc(this)">3
</form>
<span id="msg"></span>
0
 
Pravin AsarCommented:
Cool :)

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now