Show or hide table row if checked

I want to hide a table row (with input fields inside) when the 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 selected. Therefore, if the checkbox is checked at the beginning, I want the table row to be hidden. How to do it?

Please note that I know little about JavaScript, but I really need it

+4
source share
9 answers

trigger . change () an event after adding 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 am making the code shorter.

jsfiddle

+4
source

:

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

, , :

$(function() {
  var init = true;
  $('input[type="checkbox"]').change(function() {
    if (this.checked) {
      if (init) {
        $(this).prev().hide();
        init = false;
      } else $(this).prev().slideUp();
    } else $(this).prev().slideDown();
  }).change();
});
input[type='text'] {
  display: block;
  padding: 3px 5px;
  margin: 5px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input type="text" placeholder="Shown" />
  <input type="checkbox" />Hide input
</div>

<div>
  <input type="text" placeholder="Hidden" />
  <input type="checkbox" checked/>Hide input
</div>
Hide result
+2

:

$('table :checkbox').change(function(e, speed) {
    speed = typeof speed == 'undefined' ? 'slow' : 0;
    $(this).closest('tr').prev()[this.checked ? 'fadeOut' : 'fadeIn'](speed);
}).trigger('change', [0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr id="row1">
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td><input type="checkbox" id="checkbox1">Hide inputs</td>
    </tr>
        
    <tr id="row2">
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td><input type="checkbox" id="checkbox2" checked>Hide inputs</td>
    </tr>
</table>
Hide result
+1

.

 $('#checkbox').change();

$(document).ready(function () {

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

DEMO FIDDLE

+1

Musefan , !

$(document).ready(function () {
($('#checkbox').prop('checked')==true) ? $('#row').fadeOut('slow'):$('#row').fadeIn('slow');
    $('#checkbox').change(function () {
        if (!this.checked) 
            $('#row').fadeIn('slow');
        else 
            $('#row').fadeOut('slow');
    });
});
Hide result
+1

, , .

<table>
<tr id="row" style="display: none;">
    <td><input type="text"></td>
    <td><input type="text"></td>
</tr>
<tr>
    <td><input type="checkbox" id="checkbox" checked>Hide inputs</td>
</tr>
</table>
<script>
$(document).ready(function () {
$('#checkbox').change(function () {
    if (!this.checked) 
        $('#row').fadeIn('slow');
    else 
        $('#row').fadeOut('slow');
});
});
</script>
0
var showOrHideRow=fucntion(isChecked){
   if (isChecked) 
            $('#row').fadeOut('slow');
        else 
            $('#row').fadeIn('slow');
};

$(document).ready(function () {
showOrHideRow($('#checkbox').is(":checked"));

    $('#checkbox').change(function () {
        showOrHideRow(this.checked);
    });

});
0

$ ('# tbl_name tr'). find ('input: checkbox: check'). closest ('tr'). show(); $ ('# tbl_name tr'). find ('input: checkbox: Unchecked'). closest ('tr'). hide();

0

Source: https://habr.com/ru/post/1568751/


All Articles