Clicking on unit opens a table inside the modal that from the user's choice inserts the desired line in the input. But when I click on any line the whole table value is inserted. Does anyone know what I should change in javascript?
<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">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<label class="modal-title">UNIDADE</label>
</div>
<div class="modal-body">
<table id="unidade">
<?php
include ("conn.php");
echo "<table border = 2>";
echo "<tr>";
echo "<th>Unidade</th>";
echo "<th>Descrição</th>";
echo "</tr>";
$result = "SELECT codigo, descricao FROM cadunid";
$resultado = mysqli_query($conn, $result);
while($row = mysqli_fetch_assoc($resultado)) {
echo "<tr>";
echo "<td value='unidade'>". $row['codigo'] ."</td>";
echo "<td>". $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', 'table', function() {
var value = $(this).val(unidade).text();
$('.close').trigger('click');
$('.unidade').val(value);
});
</script>