I need to manipulate an element of type checkbox
according to the values in the database, if 'contact. favorite' is populated with 'on', the checkbox inside the modal should appear marked " checked
".
How can I do this with javascript?
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Favorito</th>
</tr>
</thead>
<tbody>
<?php
foreach( crud::select(select::contatos($id)) as $sql )
{
echo '<tr>';
echo '<th scope="row">#</th>';
echo "<td>".$sql['favorito']."</td>";
echo '<td><button type="button" class="btn" data-toggle="modal" data-target="#modalContatos" data-favorito="'.$sql['favorito'].'" value="">Exibir</button>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<div class="bd-example">
<div class="modal fade" id="modalContatos" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="exampleModalLabel">Modal Favorito</h4>
</div>
<div class="modal-body">
<form name="frmModal" method="post" action="../_controllers/update_contatos.php">
<div class="form-group row">
<label class="col-sm-2">Favorito: </label>
<div class="col-sm-4">
<div class="form-check">
<label class="form-check-label">Favorito</label>
<input class="form-check-input" type="checkbox" name="favorito" id="favorito">
</div>
</div>
</div>
<!--[ Form Index End ]-->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-primary">Gravar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$('#modalContatos').on('show.bs.modal', function (event) {
var favorito = button.data('favorito')
var modal = $(this)
modal.find('#favorito').val(favorito)
}</script>