简体   繁体   中英

fadeOut and fadeIn Div When Clicking Checkbox or its label

I want to fadeOut if the checkbox is unchecked, and fadeIn if the checkbox is checked. Also I have the label clickable so the text next to the checkbox should ideally do the same. Heres what I have so far, it doesn't work but I'm not sure why:

$(document).ready(function () {
    $('#frequent1').on('change', function () {
        if (!this.checked) {
            $("#self").fadeOut;
        }
    });
});

HTML:

<label>
    <input type="checkbox" id="frequent1" name="frequent1" value="self" checked> This
    item: </label>

fadeOut is a jQuery function.

This:

$("#self").fadeOut;

needs to be this:

$("#self").fadeOut();

Do some corrections in your code,

  1. create an element with id="self"

  2. fadeOut and fadeIn are not the properties, they are method in jQuery, so use fadeIn() and fadeOut() .

 $(document).ready(function () { $('#frequent1').on('change', function () { if (!this.checked) { $("#self").fadeOut(); } else{ $("#self").fadeIn(); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type="checkbox" id="frequent1" name="frequent1" value="self" checked> <span id="self">This item: </span> </label> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM