Run Javascript after AJAX call

Asked

Viewed 51 times

-1

Hello, I am loading a page into a div through AJAX, so that the data can be loaded without the need to reload the page. However, I noticed that the javascript that is on this loaded page is not executed. I need to have some scripts run only when the page referring to it is loaded into this div. I’m trying this way:

<li onclick="callref('exchange'); marcaLinha();" class="nav-item"><a href="#" class="nav-link"><i class="fa fa-exchange"></i><span>Comércio</span></a></li>

In this line, "callref" is the function that puts the "exchange" page inside the div. This function is working normally. The "bookmarkLine" function is a code snippet that is supposed to select the row of a table that is inside the exchange page, however I have the following error: Uncaught Referenceerror: markLine is not defined at Htmllielement.onclick

The "little mark" function has the following code:

function marcaLinha() {
var tabela = document.getElementById("tab_comandas");
var linhas = tabela.getElementsByTagName("tr");

for (var i = 0; i < linhas.length; i++) {
    var linha = linhas[i];
    linha.addEventListener("click", function() {
        //Adicionar ao atual
        selLinha(this, false); //Selecione apenas um
        //selLinha(this, true); //Selecione quantos quiser
    });
}

The above function was made based on a tutorial I saw on the internet, while running it in a separate file for testing it worked properly, however while doing so I am having problems.

The two functions are in the same javascript file, so I think the failure is not referring to the file import, since the function "callref" works correctly.

1 answer

0


To whom it may interest, I managed to solve the problem. I called the function right at the creation of the table, as follows:

                    <?php
                $sql = 'select * from CAD_VENDAS where STATUS=0;';
                $query = mysqli_query($mysqli, $sql);
                if (mysqli_num_rows($query) >= 1) {
                    while ($resultado = mysqli_fetch_assoc($query)) {
                        echo '
                                        <tr>
                                            <td></td>
                                            <td onclick="marcaLinha()">' . $resultado['COMANDA'] . '</td>
                                            <td onclick="marcaLinha()">' . $resultado['NOME_CLI'] . '</td>
                                        </tr>                   
                                        ';
                    }
                } else {
                    echo '<h2>Nenhum registro encontrado.</h2>';
                }
                ?>

Browser other questions tagged

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