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.
                                	
	event-handling
    
                            
                        
                    
            12,836
            questions
        
        
            3248
            votes
        
        
            14
            answers
        
        
            938k
            views
        
    event.preventDefault() vs. return false
                When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well:
1. ...
            
        
       
    
            896
            votes
        
        
            9
            answers
        
        
            2.5m
            views
        
    window.onload vs document.onload
                Which is more widely supported: window.onload or document.onload?
            
        
       
    
            759
            votes
        
        
            13
            answers
        
        
            1.2m
            views
        
    jQuery checkbox checked state changed event
                I want an event to fire client side when a checkbox is checked / unchecked:
$('.checkbox').click(function() {
  if ($(this).is(':checked')) {
    // Do stuff
  }
});
Basically I want it to happen ...
            
        
       
    
            710
            votes
        
        
            21
            answers
        
        
            1.9m
            views
        
    jQuery checkbox change and click event
                $(document).ready(function() {
  //set initial state.
  $('#textbox1').val($(this).is(':checked'));
  $('#checkbox1').change(function() {
    $('#textbox1').val($(this).is(':checked'));
  });
...
            
        
       
    
            641
            votes
        
        
            22
            answers
        
        
            420k
            views
        
    UITextField text change event
                How can I detect any text changes in a textField? The delegate method shouldChangeCharactersInRange works for something, but it did not fulfill my need exactly. Since until it returns YES, the ...
            
        
       
    
            618
            votes
        
        
            15
            answers
        
        
            243k
            views
        
    How to debug JavaScript / jQuery event bindings with Firebug or similar tools?
                I need to debug a web application that uses jQuery to do some fairly complex and messy DOM manipulation. At one point, some of the events that were bound to particular elements, are not fired and ...
            
        
       
    
            594
            votes
        
        
            2
            answers
        
        
            847k
            views
        
    How can I trigger an onchange event manually? [duplicate]
                I'm setting a date-time textfield value via a calendar widget. Obviously, the calendar widget does something like this :
document.getElementById('datetimetext').value = date_value;
What I want is:
On ...
            
        
       
    
            389
            votes
        
        
            16
            answers
        
        
            131k
            views
        
    Why are only final variables accessible in anonymous class?
                a can only be final here.  Why?  How can I reassign a in onClick() method without keeping it as private member?
private void f(Button b, final int a){
    b.addClickHandler(new ClickHandler() {
     ...
            
        
       
    
            365
            votes
        
        
            13
            answers
        
        
            444k
            views
        
    Understanding events and event handlers in C#
                I understand the purpose of events, especially within the context of creating user interfaces. I think this is the prototype for creating an event:
public void EventName(object sender, EventArgs e);
...
            
        
       
    
            319
            votes
        
        
            10
            answers
        
        
            138k
            views
        
    Difference between e.target and e.currentTarget
                I don't understand the difference, they both seem the same but I guess they are not.
Any examples of when to use one or the other would be appreciated.
            
        
       
    
            317
            votes
        
        
            9
            answers
        
        
            336k
            views
        
    jQuery - Detect value change on hidden input field
                I have a hidden text field whose value gets updated via an AJAX response.
<input type="hidden" value="" name="userid" id="useid" />
When this value changes, I would like to fire an AJAX ...
            
        
       
    
            311
            votes
        
        
            2
            answers
        
        
            449k
            views
        
    How can I capture the right-click event in JavaScript? [duplicate]
                I want to block the standard context menus, and handle the right-click event manually.
How is this done?
            
        
       
    
            306
            votes
        
        
            11
            answers
        
        
            288k
            views
        
    How to call function on child component on parent events
                Context
In Vue 2.0 the documentation and others clearly indicate that communication from parent to child happens via props.
Question
How does a parent tell its child an event has happened via props?...
            
        
       
    
            279
            votes
        
        
            14
            answers
        
        
            135k
            views
        
    Android: Difference between onInterceptTouchEvent and dispatchTouchEvent?
                What is the difference between onInterceptTouchEvent and dispatchTouchEvent in Android?
According to the android developer guide, both methods can be used to intercept a touch event (MotionEvent), ...
            
        
       
    
            277
            votes
        
        
            3
            answers
        
        
            6k
            views
        
    JavaScript click handler not working as expected inside a for loop [duplicate]
                I'm trying to learn JS and got an issue.
I tried many things and googled but all in vain. Following piece of code doesn't work as expected. I should get value of i on click but it always returns 6. I'...
            
        
       
    
            270
            votes
        
        
            10
            answers
        
        
            151k
            views
        
    What is DOM Event delegation?
                Can anyone please explain event delegation in JavaScript and how is it useful?
            
        
       
    
            266
            votes
        
        
            1
            answer
        
        
            112k
            views
        
    How to remove a lambda event handler [duplicate]
                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");
...
            
        
       
    
            254
            votes
        
        
            16
            answers
        
        
            315k
            views
        
    Which Python packages offer a stand-alone event system? [closed]
                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 ...
            
        
       
    
            247
            votes
        
        
            41
            answers
        
        
            261k
            views
        
    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 ...
            
        
       
    
            221
            votes
        
        
            7
            answers
        
        
            574k
            views
        
    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 ...
            
        
       
    
            216
            votes
        
        
            10
            answers
        
        
            380k
            views
        
    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!...
            
        
       
    
            208
            votes
        
        
            4
            answers
        
        
            35k
            views
        
    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 ...
            
        
       
    
            204
            votes
        
        
            5
            answers
        
        
            80k
            views
        
    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 ...
            
        
       
    
            195
            votes
        
        
            5
            answers
        
        
            291k
            views
        
    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 ...
            
        
       
    
            174
            votes
        
        
            8
            answers
        
        
            341k
            views
        
    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 ...
            
        
       
    
            169
            votes
        
        
            8
            answers
        
        
            198k
            views
        
    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. ...
            
        
       
    
            167
            votes
        
        
            4
            answers
        
        
            176k
            views
        
    How can I catch a ctrl-c event?
                How do I catch a Ctrl+C event in C++?
            
        
       
    
            145
            votes
        
        
            6
            answers
        
        
            327k
            views
        
    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 ...
            
        
       
    
            143
            votes
        
        
            8
            answers
        
        
            128k
            views
        
    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 ...
            
        
       
    
            139
            votes
        
        
            5
            answers
        
        
            121k
            views
        
    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"...
            
        
       
    
            135
            votes
        
        
            2
            answers
        
        
            207k
            views
        
    Pass parameter to EventHandler [duplicate]
                I have the following EventHandler to which I added a parameter MusicNote music:
public void PlayMusicEvent(object sender, EventArgs e,MusicNote music)
{
    music.player.Stop();
    System.Timers....
            
        
       
    
            132
            votes
        
        
            5
            answers
        
        
            108k
            views
        
    Explain ExtJS 4 event handling
                I've recently started learning ExtJS, and have trouble understanding how to handle Events. I have no experience of any previous versions of ExtJS. 
From reading various manuals, guides and ...
            
        
       
    
            131
            votes
        
        
            18
            answers
        
        
            84k
            views
        
    How can I change the EditText text without triggering the Text Watcher?
                I have an EditText field with a Customer Text Watcher on it. In a piece of code I need to change the value in the EditText which I do using .setText("whatever").
The problem is as soon as I make that ...
            
        
       
    
            131
            votes
        
        
            3
            answers
        
        
            108k
            views
        
    JavaScript: Listen for attribute change?
                Is it possible in JavaScript to listen for a change of attribute value? For example:
var element=document.querySelector('…');
element.addEventListener( ? ,doit,false);
element.setAttribute('...
            
        
       
    
            129
            votes
        
        
            15
            answers
        
        
            213k
            views
        
    Put length constraint in a TextField in react js
                I am taking an input from the user of the card number and wants that the length entered by user must not be less than and more than 12. Here is the declaration of my textfield.
<TextField
    id="...
            
        
       
    
            128
            votes
        
        
            9
            answers
        
        
            277k
            views
        
    Javascript event handler with parameters
                I want to make an eventHandler that passes the event and some parameters. The problem is that the function doesn't get the element. Here is an example:
doClick = function(func){
    var elem = .. // ...
            
        
       
    
            126
            votes
        
        
            2
            answers
        
        
            45k
            views
        
    Is it necessary to explicitly remove event handlers in C#
                I have a class that offers up a few events. That class is declared globally but not instanced upon that global declaration--it's instanced on an as-needed basis in the methods that need it. 
Each ...
            
        
       
    
            123
            votes
        
        
            6
            answers
        
        
            271k
            views
        
    How to trigger event when a variable's value is changed?
                I'm currently creating an application in C# using Visual Studio. I want to create some code so that when a variable has a value of 1 then a certain piece of code is carried out.
I know that I can use ...
            
        
       
    
            123
            votes
        
        
            6
            answers
        
        
            269k
            views
        
    Get clicked element using jQuery on event?
                I'm using the following code to detect when a dynamically generated button is clicked.
$(document).on("click",".appDetails", function () {
    alert("test");
});
Normally, if you just did $('....
            
        
       
    
            119
            votes
        
        
            8
            answers
        
        
            305k
            views
        
    How to remove "onclick" with JQuery?
                PHP code:
<a id="a$id" onclick="check($id,1)" href="javascript:void(0)"  class="black">Qualify</a>
I want to remove the onclick="check($id,1) so the link cannot be clicked or "check($id,...
            
        
       
    
            116
            votes
        
        
            8
            answers
        
        
            48k
            views
        
    jQuery: $().click(fn) vs. $().bind('click',fn);
                When using jQuery to hookup an event handler, is there any difference between using the click method
$().click(fn)
versus using the bind method
$().bind('click',fn);
Other than bind's optional ...
            
        
       
    
            115
            votes
        
        
            7
            answers
        
        
            78k
            views
        
    When should I use jQuery's document.ready function?
                I was told to use document.ready when I first started to use Javascript/jQuery but I never really learned why.
Might someone provide some basic guidelines on when it makes sense to wrap javascript/...
            
        
       
    
            114
            votes
        
        
            9
            answers
        
        
            107k
            views
        
    C# pattern to prevent an event handler hooked twice [duplicate]
                Duplicate of: How to ensure an event is only subscribed to once
and Has an event handler already been added?
I have a singleton that provides some service and my classes hook into some events on it, ...
            
        
       
    
            112
            votes
        
        
            13
            answers
        
        
            62k
            views
        
    Order of event handler execution
                If I set up multiple event handlers, like so:
_webservice.RetrieveDataCompleted += ProcessData1;
_webservice.RetrieveDataCompleted += ProcessData2;
what order are the handlers run when the event ...
            
        
       
    
            105
            votes
        
        
            7
            answers
        
        
            102k
            views
        
    Changing UIButton text
                So I'm trying to update the text on a UIButton when I click it. I'm using the following line to change the text:
calibrationButton.titleLabel.text = @"Calibration";
I have verified that the text is ...
            
        
       
    
            99
            votes
        
        
            4
            answers
        
        
            89k
            views
        
    event.preventDefault() vs. return false (no jQuery)
                I wondered if event.preventDefault() and return false were the same.
I have done some tests, and it seems that
If the event handler is added using old model, for example
elem.onclick = function(){
 ...
            
        
       
    
            96
            votes
        
        
            7
            answers
        
        
            66k
            views
        
    Handle URL fragment identifier (anchor) change event in Javascript
                How can I write the Javascript callback code that will be executed on any changes in the URL fragment identifier (anchor)?
For example from http://example.com#a to http://example.com#b
            
        
       
    
            93
            votes
        
        
            6
            answers
        
        
            100k
            views
        
    How to removeEventListener that is addEventListener with anonymous function?
                function doSomethingWith(param)
{
    document.body.addEventListener(
        'scroll',
        function()
        {
            document.write(param);
        },
        false
    ); // An event that ...
            
        
       
    
            91
            votes
        
        
            6
            answers
        
        
            119k
            views
        
    How to trigger function on value change?
                I realise this question has to do with event-handling and i've read about Python event-handler a dispatchers, so either it did not answer my question or i completely missed out the information.
I ...
            
        
       
    
            87
            votes
        
        
            16
            answers
        
        
            64k
            views
        
    Difference between event handlers and callbacks
                What is the difference between an event handler and a callback function?