Problem in Modal

Asked

Viewed 22 times

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">&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>

1 answer

1


In relation to the code:

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

The value variable is taking text from all elements .get-value. What is needed is to restrict this query only to "siblings" (side-by-side elements).

Try, for example:

$(document).on('click', '.get-value-codigo', function() {
    var value = $(this).siblings('.get-value').text();
    $('.close').trigger('click');
    $('.unidade').val(value);
});
  • 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 ?

  • 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

  • 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

  • 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

  • thanks really helped a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.