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

136
Visualizações
Event watcher failing on dynamically added content

Thank you in advance for any help! :)

I am using javascript to add fields to a document but the element monitor only works on hard coded fields.

For example in the code below, lozfield is monitored for change which works fine initially. However, if you click the populate button and repopulate with exactly the same input field, the watch no longer works. In debug, the field has EVENT next to it... until it is repopulated at which point the EVENT disappears. (apologies for my probably incorrect terminology!).

$("#lozfield").change(function() {
  alert("lozfield-CHANGED");
});

$("#populate").click(function() {
  document.getElementById("lozcontainer").innerHTML = '<input type="text" id="lozfield" value="ABC">';
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="lozcontainer">
  <input type="text" id="lozfield" value="ABC">
</div>
<input id="populate" type="button" value="POPULATE">

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to delegate your event to a higher element - which in your case I'd suggest is lozcontainer

$("#lozcontainer").change("#lozfield", function() {
  alert("lozfield-CHANGED");
});

$("#populate").click(function() {
  document.getElementById("lozcontainer").innerHTML = '<input type="text" id="lozfield" value="ABC">';
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="lozcontainer">
  <input type="text" id="lozfield" value="ABC">
</div>
<input id="populate" type="button" value="POPULATE">

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