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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:53:30+00:00 2026-05-23T16:53:30+00:00

Lately I’ve been trying to make an object in JavaScript with the following structure:

  • 0

Lately I’ve been trying to make an object in JavaScript with the following structure:

function colorDiv(div){
      this.div=div;
      this.div.bind("click",this.changeColor)
      this.changeColor(){
            this.div.css("background", "#FF0000");
      }
}

The problem is that the changeColor method cannot be called from the jQuery environment because this must refer to the current colorDiv object, so the bind method cannot work as expected.
How can this be solved?

  • 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-23T16:53:31+00:00Added an answer on May 23, 2026 at 4:53 pm

    There are a couple ways. The simplest is as follows:

    function ColorDiv(div) {
          var that = this;
    
          that.div = div;
          that.div.bind("click", that.changeColor);
    
          that.changeColor = function () {
                that.div.css("background", "#FF0000");
          };
    }
    
    var colorDiv = new ColorDiv($("#my-div"));
    $("#something-else").click(colorDiv.changeColor);
    

    You save a reference to this in the variable that, which is just the name commonly used in the JavaScript world for exactly this purpose. Then you refer to that instead of this inside your changeColor method. (Note that I used that everywhere, just for consistency, even though the only place it actually makes a difference is inside the changeColor method.)


    Another is to use the Function#bind method. You can either use it every time you call changeColor, like so:

    var colorDiv = new ColorDiv($("#my-div"));
    $("#something-else").click(colorDiv.changeColor.bind(colorDiv));
    

    or you can use it in the ColorDiv class to ensure that all methods are bound correctly whenever they are called:

    this.changeColor = (function () {
        this.div.css("background", "#FF0000");
    }).bind(this);
    

    As noted in the linked article, Function#bind is not supported in all browsers, so you’ll need a shim like the one they give, or possibly a full-fledged ES5 shim.

    Underscore.js has a bindAll function that could be useful here, too, especially with multiple methods:

    _.bindAll(this);
    

    Finally, it’s worth noting you don’t really need to do any of this in your particular example: just do

    this.changeColor = function () {
        div.css("background", "#FF0000");
    };
    

    instead of what you have, i.e. reference the div variable passed in, instead of the reference stored in this.div, since they are the same thing.

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

Sidebar

Related Questions

Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately, I have been doing this in an effort to speed up performance (and
So lately I have been trying ways to pass data from a child to
Lately, I saw the following line: [someObject release], someObject = nil; Why does this
Lately something weird has been happening to my projects in xcode: I've been trying
Lately I've been in the habit of assigning integer values to constants and simply
Lately I've been using XPathDocument and XNavigator to parse an XML file for a
Lately I've been taking a look at Haxe , to build an application to
Lately, I've been going through a pretty weird phase. I feel the need to
Lately I have been playing a game on my iPhone called Scramble. Some of

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.