简体   繁体   中英

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?

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 }

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>

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>

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';
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM