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

128
Visualizações
Some kind of Event scoping in Javascript?

I'm running in asp.net with javascript and jquery. I've got some javascript that runs on document ready that uses jquery to assign some onclick events with the .on function. That's fine. I also have a partial view that loads up after some user input that needs the same functionality so I'm referencing the script again in the partial view causing the document ready function to fire again and it assigns the onclick events for my controls in the partial view. That works. Except the problem is that I'm getting two onclick event for the controls on the main page causing the event to fire twice if I click on the main page controls after the partial view is loaded. I tried just adding an .off() in the jquery loop to remove the event before it's added so that the second time it runs it would remove the first one and add a new one but the event doesn't get removed. I also tried rewriting it to use .addEventListener and .removeEventLister following the same pattern; removing before adding it, but get the same results.

If I could query for the currently attached event listeners I could easily prevent the second addition of the onclick event but my research tells there's no way to do this in javascript (though you can in chrome dev tools with getEventListeners()).

So why isn't it removing the first event when I do .off() or .removeEventListener()? Is there some kind of scoping like this script that I'm calling twice, it ends up being two instances of the script and you can't remove event listeners that were applied by another script? This is just a guess.

Any guidance would be useful, thanks.

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