Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
javascript 3 state checkbox

I have a very long list of items each with a checkbox that has 3 states (Yes=ticked, No=X'd, Not Applicable=unticked). Using a checkbox I was able to toggle with 3 states using .indeterminate element, as follows:

function chkbx(cb) {
    if (cb.readOnly) cb.checked = cb.readOnly = false;
    else if (!cb.checked) cb.readOnly = cb.indeterminate = true;
    else if (cb.checked) cb.readOnly = cb.indeterminate = false;
}

function showtext() {
if (cert27.checked) {document.getElementById('BoxB27').style.display = "block";} else {document.getElementById('BoxB27').style.display = "none";}

if (!cert27.checked) {document.getElementById('BoxA27').style.display = "block";} else {document.getElementById('BoxA27').style.display = "none";}

if (cert27.indeterminate) {document.getElementById('BoxC27').style.display = "block";} else {document.getElementById('BoxC27').style.display = "none";}     
if (cert28.checked) {document.getElementById('BoxB28').style.display = "block";} else {document.getElementById('BoxB28').style.display = "none";}

if (!cert28.checked) {document.getElementById('BoxA28').style.display = "block";} else {document.getElementById('BoxA28').style.display = "none";}

if (cert28.indeterminate) {document.getElementById('BoxC28').style.display = "block";} else {document.getElementById('BoxC28').style.display = "none";}         

}
<body>
<table>
<tr>
<td><input type="checkbox" id="cert27" value="25" onclick="showtext();chkbx(this);" /></td>
<td>Banannas</td>
</tr>
<tr>
<td><input type="checkbox" id="cert28" value="15" onclick="showtext();chkbx(this);" /></td>
<td>Sugar</td>
</tr>
</table>

<span id="BoxA27" style="display:none">Bananas excluded.</span>
<span id="BoxB27" style="display:none">Bananas included.</span>
<span id="BoxC27" style="display:none">Bananas are N/A.</span>
<span id="BoxA28" style="display:none">Sugar excluded.</span>
<span id="BoxB28" style="display:none">Sugar included.</span>
<span id="BoxC28" style="display:none">Sugar is N/A.</span>

</body>

However, when changing the state of a checkbox, it not only messes up the displayed text, but also for changes the other boxes texts and does not correctly show the checkbox status.

How do I get the correct status of each checkbox, and display the correct text?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When you click on a checkbox you first need to call chkbx() to update its state and then call showtext().

Then, to handle the message displayed for each of the state, you should use if...else if...else because !checkbox.checked and checkbox.indeterminate can both be true at the same time :

function chkbx(cb) {
    if (cb.readOnly) cb.checked = cb.readOnly = false;
    else if (!cb.checked) cb.readOnly = cb.indeterminate = true;
    else if (cb.checked) cb.readOnly = cb.indeterminate = false;
}

function showtext() {

  if(cert27.checked){
    document.getElementById('BoxB27').style.display = "block";
    document.getElementById('BoxA27').style.display = "none";
    document.getElementById('BoxC27').style.display = "none";
  } else if (cert27.indeterminate) {
    document.getElementById('BoxC27').style.display = "block";
    document.getElementById('BoxA27').style.display = "none";
    document.getElementById('BoxB27').style.display = "none";
  } else {
    document.getElementById('BoxA27').style.display = "block";
    document.getElementById('BoxB27').style.display = "none";
    document.getElementById('BoxC27').style.display = "none";
  }
  
  if(cert28.checked){
    document.getElementById('BoxB28').style.display = "block";
    document.getElementById('BoxA28').style.display = "none";
    document.getElementById('BoxC28').style.display = "none";
  } else if (cert28.indeterminate) {
    document.getElementById('BoxC28').style.display = "block";
    document.getElementById('BoxA28').style.display = "none";
    document.getElementById('BoxB28').style.display = "none";
  } else {
    document.getElementById('BoxA28').style.display = "block";
    document.getElementById('BoxB28').style.display = "none";
    document.getElementById('BoxC28').style.display = "none";
  }

}
<body>
<table>
<tr>
<td><input type="checkbox" id="cert27" value="25" onclick="chkbx(this);showtext();" /></td>
<td>Banannas</td>
</tr>
<tr>
<td><input type="checkbox" id="cert28" value="15" onclick="chkbx(this);showtext();" /></td>
<td>Sugar</td>
</tr>
</table>

<span id="BoxA27" style="display:none">Bananas excluded.</span>
<span id="BoxB27" style="display:none">Bananas included.</span>
<span id="BoxC27" style="display:none">Bananas are N/A.</span>
<span id="BoxA28" style="display:none">Sugar excluded.</span>
<span id="BoxB28" style="display:none">Sugar included.</span>
<span id="BoxC28" style="display:none">Sugar is N/A.</span>

</body>

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!