Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  132] [ 7]  / answers: 1 / hits: 20563  / 10 Years ago, tue, december 23, 2014, 12:00:00

I want to hide a table row (with input fields inside) when a checkbox is checked.
I found something that works:



HTML



<table>
<tr id=row>
<td><input type=text></td>
<td><input type=text></td>
</tr>
<tr>
<td><input type=checkbox id=checkbox>Hide inputs</td>
</tr>
</table>


Script



$(document).ready(function () {
$('#checkbox').change(function () {
if (!this.checked)
$('#row').fadeIn('slow');
else
$('#row').fadeOut('slow');
});
});


Fiddle



But this only works if the checkbox is not checked already. So if the checkbox is checked at the beginning, I want the table row to be hidden. How do I do this?



Please note that I don't know much about JavaScript, but I really need this


More From » jquery

 Answers
4

trigger .change() event after you attach events:



$(function () {
$('#checkbox1, #checkbox2').change(function () {
var row = $(this).closest('tr').prev();

if (!this.checked)
row.fadeIn('slow');
else
row.fadeOut('slow');

}).change();
});


Note: I make code shorter.



jsfiddle


[#68411] Friday, December 19, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aricl

Total Points: 215
Total Questions: 91
Total Answers: 94

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
aricl questions
Mon, Aug 2, 21, 00:00, 3 Years ago
Mon, Aug 3, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Wed, Oct 23, 19, 00:00, 5 Years ago
;