Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

710
Visualizações
How do I call my javascript function when using thymeleaf?

I want to perform a subtraction where the values will come from two and subtract them, then display the result to another textbox. However, I can't seem to call my js function when using the th:onclick.

        <div class="mt-3 row g-3">
            <div class="col-md-6">
                <a th:href="@{~/records/add}"><button type="button" class="btn btn-secondary">Add Record</button></a>
                <a th:href="@{~/records/graph}"><button type="button" class="btn btn-secondary">Generate Graph</button></a>
                <a th:onclick="fnCalculate()" ><button type="button" class="btn btn-secondary">Calculate Budget</button></a>
            </div>
            <div class=" col-md-6 ">
                <h6>Total Expenses: </h6>
                <textarea class="form-control" id="totalExpense" name="totalExpense" th:text="${#aggregates.sum(records.![price])}"></textarea>

                <h6 class="mt-2">Current Budget: </h6>
                <textarea type="text" class="form-control mb-2" id="currentBudget" name="currentBudget"></textarea>

                <h6 class="mt-2">Remaining Budget: </h6>
                <textarea type="text" class="form-control mb-5" id="remainingBudget" disabled></textarea>
            </div>
        </div>
    </div>
</main>

<script th:inline="javascript">
    function fnCalculate(txt1, txt2) {
        txt1 = parseInt(document.getElementById("currentBudget").value);
        txt2 = parseInt(document.getElementById("totalExpense").value);
        document.getElementById("remainingBudget").innerHtml = txt1 - txt2;
    }
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Do not call the function, just use the function reference

<!--  Excluding the brackets `()` -->
<a th:onclick="fnCalculate" ><button type="button" class="btn btn-secondary">Calculate Budget</button></a>
            </div>

Besides, onClick will send it's own parameter [Event][1] in the fnCalculate function. so you won't get txt1 & txt2 as parameter as you expected.

Adjust your javascript implementation accordingly.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda