mardi 23 décembre 2014

Show or hide table row if checkbox is checked

I want to hide a table row (with input fields inside) when a checkbox is checked.


I found something that works:



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


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





Aucun commentaire:

Enregistrer un commentaire