javascript - How to write text below checked textbox? -
here html , jquery code. code working problem when click on 1 checkbox, text "finished" becomes visible below checkboxes instead of 1 only.
my html code is:
$('.label__checkbox').click(function() { if ($(".label__checkbox").is(':checked')) { $(".finish").css("display", "block"); } else { $(".finish").css("display", "none"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div>
you can use dom traversal method target them in current element context i.e. this
.closest()
can use traverse label
use .next()
target <span>
$('.label__checkbox').change(function() { var finish = $(this).closest('.label').next(".finish"); //var finish = $(this).closest('.center').find(".finish"); if ($(this).is(':checked')) { finish.css("display", "block"); } else { finish.css("display", "none"); } }).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div> <div class="center"> <label class="label"> <input class="label__checkbox" type="checkbox" /> <span class="label__text"> <span class="label__check"> <i class="fa fa-check icon"></i> </span> </span> </label> <span class="finish">finished</span> </div>
Comments
Post a Comment