Andrew Vanusi Andrew Vanusi - 20 days ago 10
PHP Question

Display none not working Laravel

I have a condition, if active is checked then it'll display a text form.

This is my view form code:

<div class="form-group">
<label>Active:</label>
{{ Form::checkbox('active', 0,null, array('id' =>'active_check','class' => 'active_check')) }}
</div>
<div id ="join_date" class="form-group" style="display: none;">
<label>Join Date:</label>
<div class="input-group">
{!! Form::text('join_date', null, array('class' => 'form-control datetimepicker' )) !!}
</div>
</div>


Here is my jQuery toggle and check condition code so far:

$('#active_check').click(function() {
$("#join_date").toggle(this.checked);
$('#join_date').find('input[type="text"], textarea').val('');
});
var activeCheck = $('checkbox[name="active_check"]');
function checkActive(select) {
if (select.val() == 0) {
$('#join_date').hide();
} else {
$('#join_date').show();
}
}
checkActive(activeCheck);


The issue is when it is first loaded the join_date form doesn't hide. I need to toggle the checkbox to hide it. Any idea?

Answer

Try this simple logic:

if($('#active_check').is(':not(":checked")')) {//test if active_check is not checked 
 $('#join_date').hide();//hide it
}
$('#active_check').click(function() {
    $("#join_date").toggle();//toggle it
});
Comments