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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:32:38+00:00 2026-05-26T04:32:38+00:00

I have been looking at tutorials and yet I can’t seem to figure out

  • 0

I have been looking at tutorials and yet I can’t seem to figure out where I’m going wrong. This seems like it should be very straight forward yet it’s giving me problems. Below is some simple code for creating a mouse listener for a canvas object. Currently function clickReporter is not being called when the canvas is clicked. Any ideas on why not?

HTML5

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Play Area 2 - Mouse Events and the Canvas</title>
    <script src="play_area_2.js" type="text/javascript"></script>
    </head>

    <body onload="init();">
    <canvas id="myCanvas" width="500" height="400">
    Your browser dosen't support the HTML5 canvas.</canvas><br />
    </body>
    </html>

JavaScript

    var canvas;
    var context;

    function init() {
        canvas = document.getElementById("myCanvas");
        context = canvas.getContext("2d");

        drawBox();

        canvas.addEventListener('onclick', clickReporter, false);
    }

    function clickReporter(e) {
        alert("clicked");
    }

    function drawBox() {
        context.fillStyle = "black";
        context.strokeRect(20, 20, canvas.width-20, canvas.height-20);
    }
  • 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-26T04:32:39+00:00Added an answer on May 26, 2026 at 4:32 am

    You should be using ‘click’ and not ‘onclick’. “on” is only used when setting it as a property (e.g., canvas.onclick = clickReporter).

    canvas.addEventListener('click', clickReporter, false);
    

    UPDATE

    More details on click vs. onclick.

    There are three ways to attach an event listener to an object in JavaScript:

    1. Through element.addEventListener. When you use this, you specify the event name you want to set. For example, ‘click’, or ‘mouseover’, or ‘touchstart’. In this case, you’re specifying the event’s actual name. This is useful because you can add more than one listener to an event.

      canvas.addEventListener('click', clickReporter, false);
      canvas.addEventListener('click', otherClickReporter, false);
      // Both clickReporter and otherClickReporter will be called on a click event.
      
    2. Through element.onsomeevent (onclick, onmouseover, ontouchstart). This is an older convention which is fully standard and supported in HTML5. This is a very easy way to set the event listener but it has its drawbacks. Only one event listener can be set at a time with this method:

      canvas.onclick = clickReporter;
      canvas.onclick = otherClickReporter;
      // Only otherClickReporter will be called on a click event.
      
    3. Through the HTML itself. This is where all the conventions started. You can define an event directly in the HTML very much like the previous example:

      <canvas onclick="clickReporter()"></canvas>
      

      This is the same as the following, assuming that clickReporter gets attached to the window object.

      <canvas></canvas>
      <script>
          canvasWeJustCreated.onclick = function() {clickReporter();}
      </script>
      

    Arguably the way you are doing it is the best way. There will certainly be cases where you want to use the other methods, like when generating a page on the server or trying to temporarily suppress scrolling on an iPhone, but for most applications, addEventListener is best.

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

Sidebar

Related Questions

I have been looking through Scott Guthrie's MVC tutorials and it seems like the
I'm new to LINQ to XML and I have been looking at tutorials but
I have been looking through the Java Tutorials at the Interfaces tutorial, specifically on
I have been looking following tutorials online for the past two days and I
I have been looking for tutorials demonstrating how to share a C++ object with
Hi I have been looking at certain tutorials on sharparchitecture and trying to no
I have been looking around for a solution that implements this, but google always
I've been looking into this for a while and have been unable to find
I have been looking for tutorials on how to add and remove a class
I have been looking around at tutorials on how to make an iPhone app

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.