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 8413029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:49:49+00:00 2026-06-10T00:49:49+00:00

I have been teaching myself JavaScript over the last Month now, not super consistently

  • 0

I have been teaching myself JavaScript over the last Month now, not super consistently since my work has been all over the place, when I get downtime my job is to work on extensions for our sales team.

Right now I don’t have a specific issue that i can’t solve, but I have a question that makes me think that there is something very different about functions in javascript that I am still missing.

Look at this code, and I will explain what confuses me about it:

function click(e) {
  var selection = e.target.id;
}

document.addEventListener('DOMContentLoaded', function () {
  var divs = document.querySelectorAll('div');
  for (var i = 0; i < divs.length; i++) {
    divs[i].addEventListener('click', click);
  }
});

So, in this code, I understand what is going on except how the click(e) part. The ‘e’ is an event object correct? It is not clear to me how that got passed, and how it knows that ‘e’ means that. I assume I could replace the e with “foo” and it would work still, but exactly what is happening is not clear.
I am pretty sure it has to do with this line of code:

divs[i].addEventListener('click', click);

But I don’t understand what is happening behind the scenes to make that happen the way it does.

Another example is this from the message passing at http://developer.chrome.com/extensions/messaging.html:

contentscript.js
================
chrome.extension.sendMessage({greeting: "hello"}, function(response) {
  console.log(response.farewell);
});


background.html
===============
chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

‘response’ in this is not clear to me where it is coming from, much like ‘e’ in the other example. Any help demystifying how this works would be appreciated, I am open to learning, and I haven’t found a good explanation about this.

  • 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-06-10T00:49:51+00:00Added an answer on June 10, 2026 at 12:49 am

    The event object is passed through the function by the browser itself.

    In case there is an event and a respective event handler is attached, the browser calls that event handler and passes an event object with some (more or less) relevant information about the event to the event handler.

    So with respect to your first example:

    First the function click( e ) is defined in a regular way.

    Afterwards two event handlers are registered:

    1. for the event DOMContentLoaded
    2. for a click event on multiple <div> elements.

    For the first handler an anonymous function is used.

    document.addEventListener('DOMContentLoaded', function () {
      // do stuff here
    });
    

    Here the event object is omitted as it is probably not needed.

    In the second case the <div> elements all get the same event handler, namely click(e).

    divs[i].addEventListener('click', click);
    

    Here, however, the event object is captured as a parameter by the function as it is needed inside the function body.

    In general in JavaScript you don’t have to define all parameters either in the function declaration nor in the call of a function. You just define the parameters needed and they are applied in the order given. That’s why in the first event handler’s definition the parameter for the event object can be omitted without any errors.

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

Sidebar

Related Questions

Over the last 18 months i have been hard at work teaching myself PHP
I've been teaching myself C++ and someone told me that C++ does not have
I have been teaching myself JS and Jquery primarily for GM scripts (im not
I've taught myself Obj-C, and have been self-teaching Cocoa, but adding Core Data to
I'm pretty good at Haskell and have been teaching myself J . I've read
I know this is a stupid question, but I have been teaching myself c#,
I have recently been teaching myself Haskell, and one of my exercises was to
I've been teaching myself F# for a while now. I've read Programming F# by
I'm extremely new to Java, and have mostly just been teaching myself as I
I'm teaching myself CIL and have been doing ok so far (just really started

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.