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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:09:08+00:00 2026-06-06T19:09:08+00:00

Seeking to create an event handler that registers a callback and iterates based on

  • 0

Seeking to create an event handler that registers a callback and iterates based on the event.

An early, working example is something like this:

var elt = document.getElementById("square"),
      clickCount = 0;

  // call a priviledged method with access to prviate vars from 
  // outer scope
  var count = function () {
    clickCount += 1;
    alert("you clicked me " + clickCount + " times!");
  };

  elt.addEventListener("click", count, false);

Now I want to actually write something js I could use. Here is the current construction:

//Create a class object
function CountMe () {

 //privately scoped variables
  var clickCount = 0;

 //methods on 'class' CountMe
  this.countClick = function () {
    //who's context am I in?
    this.addCount();
    alert("you clicked me " + clickCount + " times!");
  };

  this.addCount = function() {
    clickCount += 1 
  };
};

// Create an instance of countMe class
var clickMe = new CountMe();

//Add an event listener for clicks 
document.getElementById("square").addEventListener("click", clickMe.countClick ,false)

The error I receive is Uncaught TypeError: Object #<CountMe> has no method 'addEventListener'
Given an html page like this:

<html>
<body>
 <div style="width:50px; height:50px; background-color:red;" id="square">
 </div>
</body>
</html>

My questions are:

  • How should this be constructed so onclick events function as they do in the first method?
  • In the method countClick what context is the nested this in? The instance of the CountMe class I suspect, just want someone else’s take.
  • 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-06T19:09:11+00:00Added an answer on June 6, 2026 at 7:09 pm

    How should this be constructed so onclick events function as they do in the first method?

    Uncaught TypeError: Object # has no method ‘addEventListener’

    That’s obcously the reason. You need to add the event listenere to a DOM element, so you could do for example:

    var clickMe = new CountMe();
    document.getElementById("square").addEventListener("click", clickMe.countClick, false);
    

    The variable elt is, as correctly commented, privately scoped and cannot be accessed from outside. That means you just should move the add-process into the constructor (if the element is determinable), or drop it at all.

    In the method countClick what context is the nested this in? The instance of the CountMe class I suspect, just want someone else’s take.

    That depends on the calling. With your code

    clickMe.countClick();
    

    the instance clickMe would be the context, but when the function is called as an event listener the DOM element will be the context. See MDN’s overview for the this keyword. You can use a closure-scoped variable to hold a reference to the actual instance, as for example described here (there are many questions about that).

    Three possible quick-fixes:

    • domel.addEventListener("click", clickMe.countClick.bind(clickMe), false)
    • domel.addEventListner("click", function(){clickMe.countClick();}, false)
    • var that=this; this.countClick=function(){ that.addCount(); alert(); };
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I have code that looks something like this: Timer timer = new
I'm not seeing a way to create, via the HtmlHelper , a SelectListItem that
What I would like to do is create a map after an element is
I have data that looks like the following: ID | Location | Attendees |
I need a very simple and clear example of how to create an OCX
I'm starting out with DBus and event driven programming in general. The service that
I'm writing an app that displays some data which changes based upon the day
Update: After looking in the event log at around the time this occurred, I
I'm trying to create a class (in C#) that serves as an environment for
I have such task - to create control that union two controls (DataGrid 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.