Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:08:46+00:00 2026-05-16T23:08:46+00:00

I use event delegation in such way: elWraper.onclick = (function(){ //how to get here

  • 0

I use event delegation in such way:

elWraper.onclick = (function(){
    //how to get here "event" object
    e = e || window.event;
    var t = e.target || e.srcElement;

    //event handler
    return function(){
        //manipulations with "t" variable
    }
})();

how to get “event” object within the immediately executed function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:08:47+00:00Added an answer on May 16, 2026 at 11:08 pm
    elWraper.onclick = (function(){
        // misc stuff here    
    
        //event handler
        return function(e){
            e = e || window.event;
            var t = e.target || e.srcElement;
            //manipulations with "t" variable
        }
    })();
    

    In standards compliant browsers the event object is the first parameter passed into the callback function. In IE it is a global variable (which is what e = e || window.event is trying to determine). Therefore, the function that you return by the immediately executed function should accept the event object declared as its first (and usually only) argument.


    Clarification

    Since people are wondering (and probably they are wondering why the OP accepted this answer) there are uses for this that is not clear from the OP’s question. One is to create a closure to a variable to track something:

    /* Count number of clicks,
     * WITHOUT USING GLOBAL VARS!
     */
    el.onclick = (function(){
        var counter = 0;
        return function(e){
          e = e || window.event;
          var t = e.target || e.srcElement;
          counter ++;
          alert('detected '+counter+' clicks!');
          // do stuff with t or e ...
        }
    })();
    

    also, this is the classic way of assigning event handlers in loops:

    /* Use double closure to break closure in loop!
     */
    for (var i=0; i<stuff.length; i++) {
        var el = stuff[i];
        el.onclick = (function(x){
            return function(e){
              e = e || window.event;
              var t = e.target || e.srcElement;
              alert(x);
              // do stuff with t or e ...
            }
        })(i);
    }
    

    Or maybe the OP just thought that he could ‘cache’ the event object and mistakenly believed he could use this to do it. In which case, reading my explanation (instead of just the code) should enlighten the reader as to why that would be a bad idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I use jQuery event listener to handle message event, like below: $(window).on('message', function(e)
I'm looking to implement event delegation on blur/focus events, in a similar way to
I'm confused about when you should use event delegation as opposed to out-of-the-box JQuery
I am trying to use event delegation inside a containing div tag set to
I use event delegation to display ,on click,a popup with detailed info on each
I'm trying to use event object in win32 environment to synchronize two processes. Below
Why we need to use event Propagation?
I want to use Event Aggregator to allow communication between presenters on the page.
I've studied some CQRS sample implementations (Java / .Net) which use event sourcing as
I can't understand the process described in documentation. It says to use event: procedure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.