Jessica
Jessica

Reputation: 19

How to display the table by checking the checkbox using pure JavaScript?

I need to display a table when the checkbox is checked but it is not working. Below you may find my script, checkbox and table

    <script>
    function checked() {
        var checkBox = document.getElementById("check");
        var concession = document.getElementById("hidden");
        if (checkBox.checked == true){
            concession.style.display = "block";
        } else {
            concession.style.display = "none";
        }
    }
</script>

<input type = "checkbox" name = "discount" id = "check" onclick = "checked()">

<table id = "hidden" class = "center" style = "display:none">
        <tr>
            <th>102-2 Taxable income is reduced by 400AZN</th>
            <th></th>
            <th><input type = "radio" name = "400"></th>
        </tr>
        <tr>
            <th>102-3 Taxable income is reduced by 200AZN</th>
            <th></th>
            <th><input type = "radio" name = "200"></th>
        </tr>
        <tr>
            <th>102-4 Taxable income is reduced by 100AZN</th>
            <th></th>
            <th><input type = "radio" name = "100"></th>
        </tr>
        <tr>
            <th>102-5 Taxable income is reduced by 50AZN</th>
            <th></th>
            <th><input type = "radio" name = 50"></th>
        </tr>
    </table>

How can I fix this problem?

Upvotes: 0

Views: 838

Answers (4)

Tamas Szoke
Tamas Szoke

Reputation: 5542

Maybe checked() is restricted, change the name of your function:

function changed() {
  var checked = document.getElementById("check").checked;
  var concession = document.getElementById("hidden");
  if (checked) {
    concession.style.display = "block";
  } else {
    concession.style.display = "none";
  }
}
<input type="checkbox" name="discount" id="check" onChange="changed()">

<table id="hidden" class="center" style="display:none">
  <tr>
    <th>102-2 Taxable income is reduced by 400AZN</th>
    <th></th>
    <th><input type="radio" name="400"></th>
  </tr>
  <tr>
    <th>102-3 Taxable income is reduced by 200AZN</th>
    <th></th>
    <th><input type="radio" name="200"></th>
  </tr>
  <tr>
    <th>102-4 Taxable income is reduced by 100AZN</th>
    <th></th>
    <th><input type="radio" name="100"></th>
  </tr>
  <tr>
    <th>102-5 Taxable income is reduced by 50AZN</th>
    <th></th>
    <th>
      <input type="radio" name="50"></th>
  </tr>
</table>

Upvotes: 1

palaѕн
palaѕн

Reputation: 73926

You are getting this issue as checked is a reserved name. Try to rename the function name to something else like OnCheckboxClicked to fix this issue.

DEMO:

function OnCheckboxClicked() {
  var checkBox = document.getElementById("check");
  var concession = document.getElementById("hidden");
  if (checkBox.checked == true) {
    concession.style.display = "block";
  } else {
    concession.style.display = "none";
  }
}
<input type="checkbox" name="discount" id="check" onclick="OnCheckboxClicked()">

<table id="hidden" class="center" style="display:none">
  <tr>
    <th>102-2 Taxable income is reduced by 400AZN</th>
    <th></th>
    <th><input type="radio" name="400"></th>
  </tr>
  <tr>
    <th>102-3 Taxable income is reduced by 200AZN</th>
    <th></th>
    <th><input type="radio" name="200"></th>
  </tr>
  <tr>
    <th>102-4 Taxable income is reduced by 100AZN</th>
    <th></th>
    <th><input type="radio" name="100"></th>
  </tr>
  <tr>
    <th>102-5 Taxable income is reduced by 50AZN</th>
    <th></th>
    <th>
      <input type="radio" name=5 0 "></th>
        </tr>
    </table>

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

when you use inline handlers like onclick you need to assign a function, so onclick="checked" is the correct way

As a side note assigning an event via addEventListener() should be preferred

<input type="checkbox" name="discount" id="check">
<table id="hidden" class="center" style="display:none">
  ...
</table>

<script>
var concession = document.getElementById("hidden");
document.getElementById("check").addEventListener('click', function() {        
    concession.style.display = (this.checked)? "block" : "none";
});
</script>

Finally, it's worth noting that JS is not necessary at all, since you could use only CSS for this kind of task with the given markup

#check + table { display: none }
#check:checked + table { display: block }

Upvotes: 3

DumbCoder7
DumbCoder7

Reputation: 252

You have to add an event listener to the checkbox:

var checkbox = document.querySelector("#check");

checkbox.addEventListener( 'change', function() {
var concession = document.getElementById("hidden");
    if(this.checked) {
         concession.style.display='block';
    } else {
      concession.style.display='none';
    }
});

Upvotes: -3

Related Questions