This is the code for Select options:
<div class="modal-body row">
<form role="form" action="patient/addNew" class="clearfix" method="post" enctype="multipart/form-data">
<div class="form-group col-md-6">
<label for="exampleInputEmail1"><?php echo lang('categorie'); ?></label>
<select class="form-control m-bot15" name="categorie" value='' id="p_category">
<option value="#"> Sélectionner catégorie</option>
<?php foreach ($categories as $category) { ?>
<option value="category"><?php
if (!empty($setval)) {
if ($category->category == set_value('category')) {
echo 'selected';
}
}
if (!empty($patient->category)) {
if ($category->category == $patient->category) {
echo 'selected';
}
}
?> > <?php echo $category->category; ?> </option>
<?php } ?>
</select>
</div>
This is the div which must be Shown/Hidden:
<label for="exampleInputEmail1"><?php echo lang('name_Us'); ?></label>
<input type="text" class="form-control" name="name_husband" id="nameUs" placeholder="">
<label for="exampleInputEmail1"><?php echo lang('number_pre'); ?></label>
<input type="number" class="form-control" name="number_pre" id="nbreEnf" placeholder="">
</div>
And this is the JavaScript code:
$(document).ready(function () {
$('.divUs').hide();
$(document.body).on('change', '#p_category', function () {
var v = $("select.p_category option:selected").val()
if (v == 'Fe_Ence') {
$('.divUs').show();
} else {
$('.divUs').hide();
}
});
});
I would like that if the element (Fe_Ence) is selected, then the div can be displayed, otherwise, that it remains hidden
2
Answers
you can use the :has selector for example
So if a parent element has a checkbox that is checked then you can target a different child from that parent.
There are several problems with your code:
You should use #p_category option:selected instead of select.p_category option:selected in the JavaScript code and the option value for comparison should be ‘Fe_Ence’ instead of ‘category’.
Also the value attribute of the tag should be the actual category value.
For the selection section, your code should be changed as follows:
And your JavaScript code should also be changed as follows: