Modal problem

0

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">&times;</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>
    
asked by anonymous 06.10.2017 / 19:17

1 answer

1

Regarding the code:

$(document).on('click', '.get-value-codigo', function() {
    var value = $('.get-value').text();
    $('.close').trigger('click');
    $('.unidade').val(value);
});

The value variable is fetching the text of all .get-value elements. What is needed is to restrict this query to siblings only (side by side).

Try, for example:

$(document).on('click', '.get-value-codigo', function() {
    var value = $(this).siblings('.get-value').text();
    $('.close').trigger('click');
    $('.unidade').val(value);
});
    
06.10.2017 / 19:38