0

OnClick of Checkbox I want to Show/ Hide the HTML table. I tried like below

HTML

 <tr id="trfeedback" runat="server" visible="false">
            <td style="width: 5%">
            </td>
            <td style="width: 90%">
                <asp:CheckBox ID="chkFeedback" runat="server" Text="Send Feedback" Width="6%" onclick="toggleTable();" />
            </td>
            <td style="width: 5%">
            </td>
        </tr>
        <table id="trchkOptions" runat="server">
            <tr>
                <td style="width: 5%">
                </td>
                <td>
                    <asp:CheckBox ID="chkOption1" runat="server" Width="5%" />
                    Option 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To&nbsp;
                    RA 1,&nbsp; RA 2
                </td>
                <td style="width: 5%">
                </td>
            </tr>
            <tr>
                <td style="width: 5%">
                </td>
                <td>
                    <asp:CheckBox ID="chkOption2" runat="server" Width="5%" />
                    Option 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To&nbsp;
                    HMS, VMS.
                </td>
                <td style="width: 5%">
                </td>
            </tr>
            <tr>
                <td style="width: 5%">
                </td>
                <td>
                    <asp:CheckBox ID="chkOption3" runat="server" Width="5%" />
                    Option 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To,
                    RA 1,&nbsp; RA 2.
                </td>
                <td style="width: 5%">
                </td>
            </tr>
        </table>

JS

 function toggleTable() {
        var lTable = document.getElementById("chkFeedback");
        lTable.style.display = (lTable.style.display == "table") ? "none" : "table";
    }

but it is not working. I don't know where I am going wrong. The view source of the HTML is below in the fiddle

VIEW SOURCE

kindly help

Nad
  • 4,605
  • 11
  • 71
  • 160

2 Answers2

2

Below is a working version of your code which used checkboxes checked property to show hide the table.

Here are some minor changes that I made:

<!-- setting the initial state to checked and passing this to toggle function-->
<input id="chkFeedback" type="checkbox" name="chkFeedback" onclick="toggleTable(this);" />

And, then the javascript function

function toggleTable(cb) {
    var lTable = document.getElementById("trchkOptions");  
    lTable.style.display = cb.checked ? "table": "none";
}
<tr id="trfeedback">
 <td style="width: 5%">
                </td>
 <td style="width: 90%">
                    <span style="display:inline-block;width:6%;"><input id="chkFeedback" type="checkbox" name="chkFeedback" onclick="toggleTable(this);" /><label for="chkFeedback">Send Feedback</label></span>
                </td>
 <td style="width: 5%">
                </td>
</tr>
 
            <table id="trchkOptions" style='display:none'>
 <tr>
  <td style="width: 5%">
                    </td>
  <td>
                        <span style="display:inline-block;width:5%;"><input id="chkOption1" type="checkbox" name="chkOption1" /></span>
                        Option 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To&nbsp;
                        RA 1,&nbsp; RA 2
                    </td>
  <td style="width: 5%">
                    </td>
 </tr>
 <tr>
  <td style="width: 5%">
                    </td>
  <td>
                        <span style="display:inline-block;width:5%;"><input id="chkOption2" type="checkbox" name="chkOption2" /></span>
                        Option 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To&nbsp;
                        HMS, VMS.
                    </td>
  <td style="width: 5%">
                    </td>
 </tr>
 <tr>
  <td style="width: 5%">
                    </td>
  <td>
                        <span style="display:inline-block;width:5%;"><input id="chkOption3" type="checkbox" name="chkOption3" /></span>
                        Option 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To,
                        RA 1,&nbsp; RA 2.
                    </td>
  <td style="width: 5%">
                    </td>
 </tr>
</table>
 
        </table>
AKS
  • 18,983
  • 3
  • 43
  • 54
  • what is `checked` in checkbox? – Nad Apr 30 '16 at 09:51
  • 1
    It is an [attribute](http://www.w3schools.com/tags/att_input_checked.asp) of the checkbox determining whether you have selected (checked) it or not. – AKS Apr 30 '16 at 09:55
  • by default i want it **not to be checked**..Shd I remove that ? – Nad Apr 30 '16 at 09:59
  • If by default it is not checked do you want your table to be visible? – AKS Apr 30 '16 at 10:00
  • no, on page load table shd also be not visible. once i checked the table shd get visible – Nad Apr 30 '16 at 10:11
  • hi @aks, are you their? need one validation help regarding this. let me know if u can help – Nad May 03 '16 at 10:41
  • @code: I would be very happy to help although I think you need to post a new question if you have one. – AKS May 03 '16 at 11:09
  • aah, sure.will post the new question though its a simple one but I hate when people downvote it. – Nad May 03 '16 at 11:10
  • hi aks, can u help me with the validation.please. As I tried but it is not working. the above link is my question. – Nad May 04 '16 at 06:12
1

Updated the fiddle. Basically you must call window.getComputedValue to get the computed css value.

Element.style.display works only when the display is defined in the style attribute in the code.

IE8 workaround:

((window.getComputedStyle ? window.getComputedStyle(lTable) : lTable.currentStyle).display === "table") ? "none" : "table";

However I can't test it, as I don't have IE8.

Jakub Rożek
  • 2,110
  • 11
  • 12
  • its not working in `IE 8`.Sorry I forgot to mention that – Nad Apr 30 '16 at 09:30
  • still not working :( Even it is not working in `CHROME` also – Nad Apr 30 '16 at 09:36
  • In console it is showing as `frmEmployee_Exit_Interview.aspx:187 Uncaught TypeError: Cannot read property 'addEventListener' of null frmEmployee_Exit_Interview.aspx:276 Uncaught SyntaxError: Unexpected token . frmEmployee_Exit_Interview.aspx?userid=996:1006 Uncaught ReferenceError: disablegrid is not defined http://localhost:62999/ERP%20Lite/Images/FNDSSCORP.gif Failed to load resource: the server responded with a status of 404 (Not Found)` – Nad Apr 30 '16 at 09:38
  • Make sure you the code is loaded onDomReady or onLoad. In fiddle click on "javascript" and choose the correct option in "load type". – Jakub Rożek Apr 30 '16 at 09:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110708/discussion-between-jakub-rozek-and-coder). – Jakub Rożek Apr 30 '16 at 09:41