"Show/Hide div using bootstrap checkbox - require inputs to be entered only if checkbox is checked"
Bootstrap 4.1.1 Snippet by muhittinbudak

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <form id="myForm" action="#"> <div class="form-group"> <label for="email">Email address:</label> <input type="email" class="form-control" id="email"> </div> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control" id="pwd"> </div> <div class="checkbox"> <label><input type="checkbox" id="changeShip"> Show/Hide </label> </div> <div id="changeShipInputs"> <div class="form-group"> <label for="city">City</label> <input type="text" class="form-control" id="city"> </div> <div class="form-group"> <label for="state">State</label> <input type="text" class="form-control" id="state"> </div> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div>
body { padding: 15px; }
var form = $('#myForm'), checkbox = $('#changeShip'), chShipBlock = $('#changeShipInputs'); chShipBlock.hide(); checkbox.on('click', function() { if($(this).is(':checked')) { chShipBlock.show(); chShipBlock.find('input').attr('required', true); } else { chShipBlock.hide(); chShipBlock.find('input').attr('required', false); } })

Related: See More


Questions / Comments: