Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!