4

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

3
  • This type of question has been asked here so many times already... Commented Dec 23, 2014 at 13:18
  • @ChristopherW - Apparently Google is a lost art. Commented Dec 23, 2014 at 13:20
  • @jalynn2 Apparently search features in general Commented Dec 23, 2014 at 13:30

9 Answers 9

5

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

Sign up to request clarification or add additional context in comments.

Comments

4

Just call the change event after you initially register it:

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

Comments

2

Musefan's answer is excelent, but following is also another way!

$(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');
    });
});

Comments

2

I believe this is what you were looking for:

$(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>

Comments

1

Generic solution without hardcoded ids:

$('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>

Comments

1

just call the change function in document.ready after it

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

Like this

$(document).ready(function () {

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

Here is the DEMO FIDDLE

Comments

0

you can initially hide them if you really want the checkbox to be checked initially.

<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>

Comments

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);
    });

});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.