1
When clicking on a unit a table is opened inside the modal that from the user’s choice inserts the desired line in the input. But when I click on any row the whole table value is inserted. Someone knows what to 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>
Where is the unit class in html? In the JS part there is a unit variable that is not defined tbm...
– Dayglor
This is defined within the input
– Victor
the unit variable defined in the input? which is the html code of the input. .
– Dayglor
the code is this completely expensive, I just need to arrange the javascript to take the value of the columns code and description completely, and take only the value of the row I select, this is a modal of bootstrap, if it could not help a lot thank you
– Victor
Code is wrong, there is up to tr that is not being opened, but anyway. No loop while defines a class for TD that is clickable. example: class="get-text" Then in javascript you do $('. get-text'). click(Function(){ var value = $(this). text(); $('. unit'). val(value); })
– Dayglor
I checked the post and there is tr wrong, but thanks for the warning. in case I will have to create a Hidden input inside while to declare that class ?
– Victor
You edited the post 3 minutes ago. Before the tr inside the while was wrong, now you’re right.
– Dayglor
If it doesn’t work update the code and comment here that I come to look.
– Dayglor
did not work, thanks I will remove the post
– Victor
You made a mistake or you didn’t behave the way you wanted?
– Dayglor
simply does not receive nor click , I must be missing in the while loop but I give up
– Victor
Use the answer I posted, it works. When you click on the td with the get-value class it takes its value and puts it in the input.
– Dayglor