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

159
Visualizações
Ajax Listeners Below First Listener Are Not Running

This is my HTML (problem is described below):

//Form 1 is called through a modal that is disconnected from form 2:
<div class="modal">
    <form action="/user" method="POST" id="form-1">
       <input type="text" id="username" name="username">
       <button>Submit</button>
    </form>
</div>

//Form 2 is appended in #form-1 listener
<div id="user-info" class="user-details">
    <p>Username: <%= user.username %></p>
    <% if(user.email) { %>
       <p>Email: <%= user.email %></p>
    <% } else { %>
    <form action="/user?_method=PATCH" method="POST" id="form-2">
       <input type="email" id="user-email" name="user-email">
       <button>Submit</button>
    </form>
    <% } %>
</div>

The two ajax listeners/calls:

$('#form-1').submit(function(e) {
    e.preventDefault();
    const user = $(this).serialize();
    $.post('/user', user, function(data) {
        $('#user-info').append(
           `<p>Username: ${data.username}</p>
            <form action="/user?_method=PATCH" method="POST" id="form-2">
            <input type="email" id="user-email" name="user-email">
            <button>Submit</button>
            </form>`
        )
    });
});

$('#form-2').submit(function(e) {
    e.preventDefault();
    const user = $(this).serialize();
    $.ajax({
        url: '/user',
        data: user,
        type: 'PATCH',
        success: function(data) {
            $('#user-info').append(
           `<p>Username: ${data.username}</p>
            <p>Email: ${data.email}</p>`
        )
        }
    });
});

The first one (#form-1) runs without a problem. However, the second one (#form-2) is never run even though it is the only one that is called. It only works when I comment out the first one (in other words, when I place it in the beginning of the script file. I think the second one is being ignored. Is there a way to listen to both forms simultaneously?

about 4 years ago · Juan Pablo Isaza
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