Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Triple state checkbox

Status
Not open for further replies.

deeciple

Technical User
Mar 1, 2012
70
US
Hi All,

I have a search form that has a checkbox on it. I want the user to be able to query the database for this checkbox in three states, as follows:

1. The box is checked (return records where only the box is checked).
2. The box is not checked (return records where only the box is not checked).
3. The box is indeterminant, neither checked or unckecked (return records where both the box is checked and unchecked).

I know that there is such a thing as this called a triple state checkbox and I have used them before but I don't know how to create one in javascript. Some of the solutions I have researched seem very complicated and I have tried a small function to accomplish this on my own but it is not working. Can someone have a look and help me see whaere it is going wrong?
JavaScript:
function triState(elem) {

  var chkbox = document.getElementById(elem.id);

  if (true == chkbox.checked) { //uncheck the box and set value to 0
    checkbox.value = 0;
    chkbox.checked = false;

  }else if (true != chkbox.checked) { //make the box opaque and set value to nothing
    chkbox.value = "";
    checkbox.style.filter = "alpha(opacity=" + opacityValue*100 + ")"; // IE
     
  }else{ //check the box and set value to -1
    chkbox.value = -1;
    chkbox.checked = true;}
}

Thanks in advance,

Ken
 
Tri-state checkboxes do not exist in HTML and a HTML checkbox will only pass a value in the HTTP POST/GET values when it is checked. So server side a value for that field
So checkboxes are only ever checked or unchecked (object.checked = true|false)

To emulate a "tri-state" checkbox you also need a hidden form field to hold the values rather than the checkbox itself.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks Chris,

That hidden field worked a treat!

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top