Function to hide table cells depending on whether checkbox is checked or not
Hi, I am trying to write a Javascript function that checks whether certain checkboxes are checked or not, and then hides cells in a table if the corresponding checkbox is not checked, and makes cells visible if the corresponding checkbox is checked.
The above html gives a (3 rows x 2 columns) table where the checkbox in cell 1x1 (row1,column1) starts checked and corresponding cell 1x2 is visible - as required. However when checking or unchecking any of the checkboxes (which should run the javascript) the cell visibility is not changed.
Can anyone see what I have done wrong? (probably something stupid, I'm new to this stuff)
I haven't copied and attempted to run your code, but the "checked" property for an inbox element is a boolean rather than an integer. Perhaps "1" can substitute for "true,"--I don't know. But instead of "if (check.checked == 1)" you might put "if (check.checked === true)," and don't put any quotes around true.
in the onClick even you should be passing strings
many of your HTML properties are not enclosed in quotes
Your naming is inconsistent (in some places there is Cell1 others it is cell1)
etc etc
On the other hand: the "hiding" (by visibility or by display) of a form's element (or its parent node) does not mean, automatically, that the element will not submit its value (if any) onsubmit. When you try to hide a form's element, make sure it is also disabled at the same time.
Oh yeah--one other thing. Put single quotes around the arguments you're passing to the Javascript in your HTML, such as onclick="ShowHideCell('check1','cell1')"> rather than onclick="ShowHideCell(check1,cell1)">. Even if neglecting to do this will work for a particular browser, you're setting yourself up for potential cross-browser incompatability problems--the bane of Javascript programming.
Bookmarks