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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:50:06+00:00 2026-06-02T04:50:06+00:00

I’m learning how to manipulate events in JavaScript and I’m wondering "why do you

  • 0

I’m learning how to manipulate events in JavaScript and I’m wondering "why do you have to pass the event object as a parameter (argument) into a function when using event handling?"

Here’s an example of what I am talking about:

<script type="text/javascript">
    document.getElementById('button_1').onclick = (function (event) {
        alert("The event is: " + "on" + event.type);
    });
</script>

I wrote the code above and I pretty much understand what it does. I just don’t understand the whole (event) passing. I thought of this as a way of assigning an anonymous function to the button_1.onclick event handler. Does the event handler try to pass in an event before it gets assigned or?… I’m having a difficult time understanding this. If someone could please clarify this for me I would be grateful.

[I tried searching it on Google but found very complex explanations and examples. Only a simple-to-intermediate explanation would help.] =)

  • 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-02T04:50:07+00:00Added an answer on June 2, 2026 at 4:50 am

    The Ever-Present Event, Whether You Like it or Not

    The event is always present, even when you don’t provide a name:

    $(".foo").on("click", function(){
      alert( arguments[0].type );
    });
    

    That is the same as saying this:

    $(".foo").on("click", function(event){
      alert( event.type );
    });
    

    The event object is already being passed to your callback (whether your provide a name for it or not), you can choose to not use it if you like. For instance, if we looked to a jQuery onClick method:

    $(".foo").on("click", function(){
      /* Do stuff */
    });
    

    Making Use of It

    You’ll note that I have no event object referenced in my callback. I’m not required to. However, if I want to use it, for whatever purpose, I should give it a name:

    $(".foo").on("click", function(myEvent){
      myEvent.preventDefault();
      myEvent.stopPropagation();
    });
    

    Now that I have granted myself access to the event details, I can prevent the default behavior that would result from the event, and I can also stop the event from bubbling up the DOM to other elements.

    Practical Example

    Suppose we wanted to listen for click events on an element:

    $("#bigSquare").on("click", function(event){
      /* Do something */
    });
    

    Click events happen on an element when you click the element itself, or any of its children. Now suppose this element had two children:

    <div id="bigSquare">
      <div id="redSquare"></div>
      <div id="blueSquare"></div>
    </div>
    

    Clicking any of these, the big square, the red square, or the blue square will cause the “click” event on the big square – after it causes the click event on whichever element you clicked first (events bubble up the DOM).

    We could determine which element was the target in any click event via the event itself:

    $("#bigSquare").on("click", function(event){
      alert( event.target.id );
    });
    

    Note here how we’re accessing the ID of the target that raised the event. If you click on the red square, when that event bubbles up to the big square, we will see alerted “redSquare”. The same goes for the blue square. If you click that, the event will bubble up to the big square and we will see alerted “blueSquare”.

    You can test this online via the following demo: http://jsbin.com/ejekim/edit#javascript,live

    Try clicking the orange, red, or blue square to see what is alerted.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from

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.