javascript - Jquery/JS Show dynamic checkbox input group title if at least one or more are checked and vice versa -
i want show input group title if @ least 1 checkbox checked, , hide title if no checkboxes checked. each group has own title , data-value. there may 5+ input groups.
here codepen: [example code here][1] [1]: https://codepen.io/anon/pen/jyrqgw
$(document).ready(function(){ });
.output { margin-top:45px; }
<div class="group-block" data-value="1"> <h1 class="group-title" data-value="1">group1</h1> <input type="checkbox" data-value="1">item1 <input type="checkbox" data-value="1">item2 <input type="checkbox" data-value="1">item3 <input type="checkbox" data-value="1">item4 </div> <div class="group-block" data-value="2"> <h1 class="group-title" data-value="2">group2</h1> <input type="checkbox" data-value="2">item1 <input type="checkbox" data-value="2">item2 <input type="checkbox" data-value="2">item3 <input type="checkbox" data-value="2">item4 </div> <div class="output">here show titles</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
enter code here
at end, i'm not js/jquery trying make work few hours , have feeling go deep there 2 line solution.
thanks help
i looking for?
$(document).ready(function() { check() }); $("input[type=checkbox]").change(function() { check() }) function check() { $(".group-block").each(function() { $(this).find(".group-title").css("display", $(this).find("input[type=checkbox]:checked").length ? "block" : "none") }) }
it check if checkbox has been checked on both .ready()
, .change()
statement, , show or hide title depends on result.
$(document).ready(function() { check() }); $("input[type=checkbox]").change(function() { check() }) function check() { $(".group-block").each(function() { $(this).find(".group-title").css("display", $(this).find("input[type=checkbox]:checked").length ? "block" : "none") }) }
.output { margin-top: 45px; }
<div class="group-block" data-value="1"> <h1 class="group-title" data-value="1">group1</h1> <input type="checkbox" data-value="1">item1 <input type="checkbox" data-value="1">item2 <input type="checkbox" data-value="1">item3 <input type="checkbox" data-value="1">item4 </div> <div class="group-block" data-value="2"> <h1 class="group-title" data-value="2">group2</h1> <input type="checkbox" data-value="2">item1 <input type="checkbox" data-value="2">item2 <input type="checkbox" data-value="2">item3 <input type="checkbox" data-value="2">item4 </div> <div class="output">here show titles</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
demo 2 coment
$(document).ready(function() { check() }); $("input[type=checkbox]").change(function() { check() }) function check() { $(".output").text(""); $(".group-title").hide(); $(".group-block").each(function() { if ($(this).find("input[type=checkbox]:checked").length) $(".output").text($(".output").text() + $(this).find(".group-title").text()) }) }
.output { margin-top: 45px; }
<div class="group-block" data-value="1"> <h1 class="group-title" data-value="1">group1</h1> <input type="checkbox" data-value="1">item1 <input type="checkbox" data-value="1">item2 <input type="checkbox" data-value="1">item3 <input type="checkbox" data-value="1">item4 </div> <div class="group-block" data-value="2"> <h1 class="group-title" data-value="2">group2</h1> <input type="checkbox" data-value="2">item1 <input type="checkbox" data-value="2">item2 <input type="checkbox" data-value="2">item3 <input type="checkbox" data-value="2">item4 </div> <div class="output">here show titles</div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Comments
Post a Comment