Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  148] [ 5]  / answers: 1 / hits: 20227  / 8 Years ago, wed, june 29, 2016, 12:00:00

Ok, I've been having a really weird problem using a checkbox which collapses a hidden div using bootstrap.



if I have data-toggle=collapse in the checkbox input attribute section, the Div Collapses but requires that every single one of the inputs inside it be filled out.



If data-toggle=collapse is not there, the hidden div doesn't collapse, and if the checkbox is checked it requires the inputs to be entered and if it's left unchecked I can submit the form without the inputs being entered. (desired action, but the div doesn't hide or show when the checkbox is checked)



How do I hide/show the div when the checkbox is unchecked/checked AND only require the inputs if the box is checked?



I'm using this as the HTML:



                    <div class=col-md-1>
<input type=checkbox onclick=ChangeShip() href=#moreabout data-toggle=collapse aria-expanded=false aria-controls=moreabout class=form-control id=chShipAdd name=chShipAdd value=no>
</div>
<label for=chShipAdd class=col-md-3 control-label>Shipping Information?</label>
<div id=shipadddiv style=visibility: hidden;>
<div class=collapse id=moreabout >
<div class=form-group>
<div class=col-md-12>
<br>
<input id=sStreet name=sStreet type=text placeholder=Street Name (required) class=form-control shipClass required>
</div>
</div>
<div class=form-group>
<div class=col-md-4>
<input id=sCity name=sCity type=text placeholder=City (required) required class=form-control shipClass>
</div>
<div class=col-md-4>
<input id=sState name=sState type=text placeholder=State (required) required class=form-control shipClass>
</div>
<div class=hidden-lg hidden-md>&nbsp;</div>
<div class=col-md-4>
<input id=sZipcode name=sZipcode type=text placeholder=Zip (required) required class=form-control shipClass>
</div>
</div>
</div>
</div>


and the javascript:



function ChangeShip() {
if (!(document.getElementById('chShipAdd').checked)) {
document.getElementById('shipadddiv').style.visibility=hidden;
$(.shipClass).prop(disabled,true);
}
else {
document.getElementById('shipadddiv').style.visibility=visible;
$(.shipClass).prop(disabled,false);
}
}


Any solution that WORKS will be acceptable. I've bashed my brain all day trying to do this simple action. I've tried .prop .attribute .setAttribute .removeAttribute, and much much more.



Any Advice?


More From » jquery

 Answers
24

You can use jquery to solve this quickly. You can wrap your inputs for change ship and give it and id. And let jquery do the rest.



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


See this jsfiddle for your problem. This should help you.


[#61585] Monday, June 27, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gageherberth

Total Points: 249
Total Questions: 115
Total Answers: 119

Location: Liechtenstein
Member since Sun, Sep 12, 2021
3 Years ago
;