I have a table of my modal and wanted the user to click both Code and Description to insert only the Code in my input
, however I am trying and unsuccessful because clicking any line in the Description column is inserted all Codes in the input
field
index.php
<div class="container">
<h2>Modal</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Unidade</button>
<input type="text" class="form-control unidade">
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<label class="modal-title">UNIDADE - clique sobre a unidade desejada</label>
</div>
<div class="modal-body">
<table id="get-value" border="2">
<tr>
<th> Unidade </th>
<th> Descrição</th>
</tr>
<?php
include ("conn.php");
$result = "SELECT codigo, descricao FROM cadunid";
$resultado = mysqli_query($conn, $result);
while ($row = mysqli_fetch_assoc($resultado)){
echo "<tr>";
echo "<td class='btn-default get-value'> ". $row['codigo'] ." </td>";
echo "<td class='btn-default get-value-codigo'> ". $row['descricao'] ." </td>";
echo "</tr>";
}
?>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).on('click', '.get-value', function() {
var value = $(this).text();
$('.close').trigger('click');
$('.unidade').val(value);
});
$(document).on('click', '.get-value-codigo', function() {
var value = $('.get-value').text();
$('.close').trigger('click');
$('.unidade').val(value);
});
</script>