0
I have a table of my modal and I wanted that if the user click both Code and Description insert only the Code in my input
, however I am trying and without success, because when clicking on any row of the column Description is inserted all Codes in the field input
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>
I got it, thanks man. But can I ask you a question that came up? If I have a table with 6 elements, I would have a simpler way than making 6 scripts pointing to the element I want to fill in the input ?
– Victor
That code I gave you, you wrote it six times? If you do it using relative paths (Parent, siblings, Children, etc.), you can do it in a generic way without having to repeat 6 times
– Eduardo
not in this case I only have two fields so I only used in the field below that was referring to column description, but if I have 6 columns and it is necessary that when clicking on any row referring to the client code enter the client code in the input understands
– Victor
Yes, in this case you can put the same class (get-value-code) in all columns and the code will automatically take the value of the column with get-value class, without the need to replicate the script
– Eduardo
thanks really helped a lot
– Victor