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

  • Home
  • SEARCH
  • 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 6247923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:57:49+00:00 2026-05-24T12:57:49+00:00

Here’s my code, it’s based off the example provided at MDN: window.onload = function(){

  • 0

Here’s my code, it’s based off the example provided at MDN:

window.onload = function(){
    var element = document.createElement('div');

    var event = document.createEvent("HTMLEvents");
    event.initEvent("myClick", true, false);

    element.dispatchEvent(event);

    document.addEventListener("myClick", function(){
        alert("myClick event caught");
    }, false);
}

When I run this, nothing happens, indicating something went wrong in the creation, sending or catching of the event. I would appreciate any help in understanding where I am going wrong here.

  • 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-24T12:57:51+00:00Added an answer on May 24, 2026 at 12:57 pm

    @Pablo Fernandez is right in his answer about the order, but another component is that your element needs to be in the DOM.

    The reason is that you’re making the event a bubbling event, and attaching the handler to the document. Well in order for the event to bubble up to the document, the element it’s bubbling from needs to be within the document.

    Example: http://jsfiddle.net/nhsN4/

    window.onload = function(){
        var element = document.createElement('div');
    
            // add element to the DOM
        document.body.appendChild( element );
    
        var evt = document.createEvent("HTMLEvents");
    
        evt.initEvent("myClick", true, false);
    
            // add listener to the document
        document.addEventListener("myClick", function(){
            alert("myClick event caught");
            alert( event.type );
        }, false);
    
            // dispatch the event on the element, and it bubbles up to the document
        element.dispatchEvent(evt);
    };
    

    If you had added the handler directly to the element you created, then you could dispatch the event without it being in the document.

    Example: http://jsfiddle.net/nhsN4/1/

    window.onload = function(){
        var element = document.createElement('div');
    
        var evt = document.createEvent("HTMLEvents");
    
        evt.initEvent("myClick", true, false);
    
        // add listener to the element
        element.addEventListener("myClick", function(){
            alert("myClick event caught");
            alert( event.type );
        }, false);
    
        element.dispatchEvent(evt);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code. My question has to do with the append function inside
Here is my code, which takes two version identifiers in the form 1, 5,
Here is a link my example of the misaligned table rows Click preview in
I have found this example on StackOverflow: var people = new List<Person> { new
here a a piece of code that is supposed to loop over and over
Here is my code: PtyView *v = [[PtyView alloc] init]; [v sendData([charlieImputText stringValue])]; in
Here is an example table: ID time data type 0 0100 xyz 0 1
Here is a synopsis of my code which is a moderately complex WinForms GUI.
Here's my code $string = preg_replace(/rad\:([0-9]+)px\;\s+\/\*\sALT\[(.+)\*\/|rad\:([0-9]+)px\;/,($2?$2:$1),$string); Basically, in the regex I've got a pipe
here are 2 screen shots when i try to debug my code in visual

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.