Questions tagged [event-handling]

Event handling is a coding style about handling messages between a source and one or more subscribers. A point listener in the source provide a way which subscribed code can consume messages raised from the source.

12746 questions
267
votes
10 answers

What is DOM Event delegation?

Can anyone please explain event delegation in JavaScript and how is it useful?
Xenon
  • 2,687
  • 3
  • 16
  • 4
265
votes
1 answer

How to remove a lambda event handler

I recently discovered that I can use lambdas to create simple event handlers. I could for example subscribe to a click event like this: button.Click += (s, e) => MessageBox.Show("Woho"); But how would you unsubscribe it?
Svish
  • 152,914
  • 173
  • 462
  • 620
253
votes
16 answers

Which Python packages offer a stand-alone event system?

I am aware of pydispatcher, but there must be other event-related packages around for Python. Which libraries are available? I'm not interested in event managers that are part of large frameworks, I'd rather use a small bare-bones solution that I…
Josip
  • 6,677
  • 9
  • 26
  • 27
243
votes
41 answers

Detecting Browser Autofill

How do you tell if a browser has auto filled a text-box? Especially with username & password boxes that autofill around page load. My first question is when does this occur in the page load sequence? Is it before or after document.ready? Secondly…
Undefined
  • 11,234
  • 5
  • 37
  • 62
217
votes
7 answers

jQuery equivalent of JavaScript's addEventListener method

I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element, true); I've gotten as far as: $(document).click(select_element); but that doesn't achieve the same result, as the last…
Bungle
  • 19,392
  • 24
  • 79
  • 106
207
votes
10 answers

How can I remove a JavaScript event listener?

I'm trying to remove an event listener inside of a listener definition: canvas.addEventListener('click', function(event) { click++; if(click == 50) { // remove this event listener here! } // More code here ... How could I do…
thomas
  • 2,071
  • 2
  • 13
  • 3
205
votes
4 answers

Do event handlers stop garbage collection from occurring?

If I have the following code: MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass = null; Will pClass be garbage collected? Or will it hang around still firing its events whenever they occur? Will I need to do the following in…
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
201
votes
5 answers

Why and How to avoid Event Handler memory leaks?

I just came to realize, by reading some questions and answers on StackOverflow, that adding event handlers using += in C# (or i guess, other .net languages) can cause common memory leaks... I have used event handlers like this in the past many…
gillyb
  • 8,760
  • 8
  • 53
  • 80
188
votes
5 answers

How to pass event as argument to an inline event handler in JavaScript?

// this e works document.getElementById("p").oncontextmenu = function(e) { e = e || window.event; var target = e.target || e.srcElement; console.log(target); }; // this e is undefined function doSomething(e) { e = e || window.event; var…
user1643156
  • 4,407
  • 10
  • 36
  • 59
171
votes
8 answers

How do I add a simple onClick event handler to a canvas element?

I'm an experienced Java programmer but am looking at some JavaScript/HTML5 stuff for the first time in about a decade. I'm completely stumped on what should be the simplest thing ever. As an example I just wanted to draw something and add an event…
Jer
  • 5,468
  • 8
  • 34
  • 41
163
votes
8 answers

How can I be notified when an element is added to the page?

I want a function of my choosing to run when a DOM element is added to the page. This is in the context of a browser extension, so the webpage runs independently of me and I cannot modify its source. What are my options here? I guess that, in…
162
votes
4 answers

How can I catch a ctrl-c event?

How do I catch a Ctrl+C event in C++?
Scott
  • 5,135
  • 12
  • 57
  • 74
145
votes
6 answers

Handling a Menu Item Click Event - Android

I want to create an intent that starts a new activity once a Menu Item is clicked, but I'm not sure how to do this. I've been reading through the android documentation, but my implementation isn't correct..and some guidance in the right direction…
143
votes
8 answers

jQuery: more than one handler for same event

What happens if I bind two event handlers to the same event for the same element? For example: var elem = $("...") elem.click(...); elem.click(...); Does the last handler "win", or will both handlers be run?
flybywire
  • 261,858
  • 191
  • 397
  • 503
135
votes
5 answers

Detecting CTRL+C in Node.js

I got this code from a different SO question, but node complained to use process.stdin.setRawMode instead of tty, so I changed it. Before: var tty = require("tty"); process.openStdin().on("keypress", function(chunk, key) { if(key && key.name ===…
user3025492
  • 2,853
  • 4
  • 18
  • 19