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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:13:34+00:00 2026-06-13T00:13:34+00:00

JSFiddle: http://jsfiddle.net/M2ALY/3/ My goal is to make a module that I can use and

  • 0

JSFiddle: http://jsfiddle.net/M2ALY/3/

My goal is to make a module that I can use and distribute. Therefore I must not pollute the global namespace. The module I’m making is also going to be used multiple times on one web page. That’s why I chose to use OOP, but this introduced a problem.

I want my object to bind a function to be run when the user clicks an element in the DOM. In this simplified example I made, I want an alert box to pop up when the user clicks a paragraph. As an example, one of the things I need in the real project I’m working on is: The user clicks a canvas, the function figures out where the user clicked and saves it to this.clientX and this.clientY.

Instead of doing

this.bind = function() {
    $("p1").bind('click', function() {
        // code here
    });
}

I figured it would work if I did:

this.bind = function() {obj.codeMovedToThisMethod()}

The problem is that this isn’t a good design. Inside the “class” you shouldn’t need to know the name of the object(s) that is going to be made of this “class”. This doesn’t get better when I’m making multiple objects of the “class”…

So I figured I could do

$("p1").bind('click', function(this) {
        // code here
    });
}

But it didn’t work because sending this into the function didn’t work as I thought.

How should I solve this problem?
Here is a simplified sample problem. (Same as JSFiddle.)

var test = function() {
    this.alert = function() {
        alert("Hi");
    }

    this.bind = function() {
        $("#p1").bind('click', function() {
            obj.alert();
        });
    }        
}

window.obj = new test();
obj.bind();

// What if I want to do this:

var test2 = function() {
    // Private vars
    this.variable = "This secret is hidden.";

    this.alert = function() {
        alert(this.variable);
    }

    this.bind = function() {
        $("#p2").bind('click', function(this) {
            obj2.alert();
            this.alert();
        });
    }        
}

window.obj2 = new test2();
obj2.bind();​

Thanks!

  • 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-13T00:13:35+00:00Added an answer on June 13, 2026 at 12:13 am

    Read MDN’s introduction to the this keyword. As it’s a keyword, you can’t use it as a parameter name.

    Use either

    this.bind = function() {
        var that = this;
        $("#p2").on('click', function(e) {
            that.alert();
            // "this" is the DOM element (event target)
        });
    }
    

    or $.proxy, the jQuery cross-browser equivalent to the bind() function:

    this.bind = function() {
        $("#p2").on('click', $.proxy(function(e) {
            this.alert();
        }, this));
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jsfiddle: http://jsfiddle.net/FgZnK/1/ Clicking on the box goes to a page not found. How can
Here is the jsFiddle: http://jsfiddle.net/JFwDw/2/ What I'm wanting to do is use links to
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
JSfiddle: http://jsfiddle.net/ybZvv/57/ I have a fiddle here where the user can append row and
Please check the following JSFiddle: http://jsfiddle.net/rhulshof/6bd4g/4/ I'm experiencing 2 problems which I just can't
JSFiddle: http://jsfiddle.net/QbyUR/ So, I have an empty TD, that I'm trying to remove the
I have the following jsFiddle: http://jsfiddle.net/tad604/dpRca/2/ That works as desired on IE and Chrome,
Please check out this jsfiddle http://jsfiddle.net/rr4GH/1/ As you can see its a round edge
In this JSFiddle http://jsfiddle.net/littlesandra88/mzPxN/ am I trying to use the tablesorter plugin to sort
jsFiddle : http://jsfiddle.net/brandondurham/g3exx/ How can I create relationships between various observableArrays in my model?

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.