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

148
Visualizações
Event not working on a block added via ajax

I use thymeleaf with js.

<form class="row" id="testamentExecutor">
    <div id="executorsSection" class="form-row">
        <div th:insert="fragments/executor :: executor" th:remove="tag" />
    </div>
    <div id="executorSectionAdd" class="deleteButton">
        <a href="#" class="link-primary" id="addExecutor">Add</a>
   </div>
</form>

Code of framents/executors

https://pastebin.com/L1FtFM8V

Click event on addExecutor allow to add another bloc of executorSection. AlternativeExecutor section is include in executorSection but it's hided.

<script type="text/javascript" th:inline="javascript">
    $(document).ready(function() {

        $(".delete-executor").on('click', function (e) {
            e.preventDefault();
            $(this).parent().parent().remove();
        });

        $("#addExecutor").on('click', function (e) {
            e.preventDefault();
            $.ajax({
                url: "/executor",
                success: function (data) {
                    $("#executorsSection").append(data);
                    $("#executorSection, #executorSectionAdd, .deleteButton").removeClass('visually-hidden');
                    let nbExecutor = $('.executorSection').length;
                    if (nbExecutor == 2) {
                        $("#executorSectionAdd").addClass('visually-hidden');
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                }
            });
        });
    });
</script>

When I add another executor, it's event (delete-executor, addAlternativeExecutor, delete-alternative-executor) is not working for them

enter image description here

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

If you're adding new elements to the DOM after the definition of your event listeners, the event listeners will not work on the newly added elements.

You can add event listeners only to elements that already exist in the DOM at the moment when listeners are defined.

Each time you add a new element to the DOM you have to attach a new event listener to it (if you need to have one).

about 4 years ago · Santiago Gelvez Relatório

0

when you call $(".delete-executor").on('click' you are calling it on the currently existing elements.

adding another .delete-executor later will not add the event to that new element automatically.

but, you can change that to $("#executorsSection").on('click', '.delete-executor', instead. now the event is on the container instead of the element itself.

about 4 years ago · Santiago Gelvez 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