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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:29:07+00:00 2026-06-17T05:29:07+00:00

Is this the way to properly bind and event to an object’s method in

  • 0

Is this the way to properly bind and event to an object’s method in JavaScript using jQuery?

I’ve set up a little example code but the part I’m concerned of is the two-lines after the comment “is this ok?“

Of course, as the callback is a method of the object, I need the context to remain the same.

function MyPrototype(id) {

    this.id = id;
    this.sel = '#' + id;

    // *** IS THIS OK? ***
    $(this.sel).on('click', function(evt) {
        MyPrototype.prototype.mouseClick.call(this, evt); });
}

MyPrototype.prototype.mouseClick = function (evt) {

    // I want to use evt to get info about the event
    // I want use this to access properties and methods of the instance

    alert(this.id + ' was clicked');
}

myObject1 = new MyPrototype('myDiv1');
myObject2 = new MyPrototype('myDiv2');

Also, I may come to the necessity of unbinding the event from the specific function.

But the following is not working…

MyPrototype.prototype.unbindClick = function() {

    $(this.sel).off('click', function(evt) {
        MyPrototype.prototype.mouseClick.call(this, evt); });
}

myObject2.unbindClick();

Note that I’m passing an inline function as the event handler.

  • 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-17T05:29:09+00:00Added an answer on June 17, 2026 at 5:29 am

    Try jQuery.proxy:

    function MyPrototype(id) {
        this.id = id;
        this.sel = '#' + id;
    
        // using jQuery.proxy:
        $(this.sel).on('click', $.proxy(this.mouseClick, this));
    
        // or Function.bind:
        // $(this.sel).on('click', this.mouseClick.bind(this));
    
        // or writing it out:
        /*
        var self = this;
        $(this.sel).on('click', function () {
          return self.mouseClick.apply(self, arguments);
        });
        */
    }
    
    MyPrototype.prototype.mouseClick = function(evt) {
    
        // I want to use evt to get info about the event
        // I want use this to access properties and methods of the instance
    
        console.log(this.id + ' was clicked');
    };
    
    var myObject1 = new MyPrototype('myDiv1');
    var myObject2 = new MyPrototype('myDiv2');
    

    http://jsbin.com/axokuz/1/


    About the update to the question:

    If you want to unbind a single event handler you would need the exact same handler function used while binding. Otherwise the whole event would be unbound. Neither the solution you added to the question nor $.proxy would help with that. There are some solutions, though:

    • keep a reference to the bound handler and use that while unbinding
    • use jQuery’s Namespaced Events
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to add an event listener/handler to a JavaScript object? Preferably
I send data in this way: <form method=post enctype=text/plain action=Func> And I have a
I have a menu set up this way: <div id='top-navigation'> <ul> <li><a class='top-nav-button-unclicked' href='#'>SUBJECT</a></li>
I am using the jQuery clone() method to clone a table row, then using
In my Silverlight prjoect I am creating a simple two-way binding in code: This
I'm trying to bind a 1-many mapping using KnockoutJS, where 1 zip code can
Is there any way to create a property like this C# property in Objective-C?
Consider this way of solving the Subset sum problem: def subset_summing_to_zero (activities): subsets =
I populated comboboxes in this way foreach (Control c in this.Controls) { if (c
There is this way, of course: OuterClass.this . But that's very clumsy. Ideally, there'd

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.