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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:04:59+00:00 2026-05-30T05:04:59+00:00

I created an object and it works great when I have one instance of

  • 0

I created an object and it works great when I have one instance of the object. This isn’t the actual Object, but the concept is the same.

function foo(name){
    this.name = name;
}

foo.prototype.CreateElement = function(){
    var newElement = document.createElement("a");

    that = this;

    newElement.onclick = function(){
        that.SayName(this);
    };

    document.getElementsByName("body")[0].appendChild(newElement);
}

foo.prototype.SayName = function(LinkObj){
    alert(this.name);
    LinkObj.href = "http://google.com";
}

I was told that by saving the instance of this to a variable (in this case that), I would still get a reference to the correct object instance for the SayName method. This would leave this to represent the instance of the element passed in from the onclick. This all seems to work properly with only one instance, but when I create multiple instances of the object I end up with the last instance being the instance supplied in the onclick event.

I initialize two objects like this

var ObjOne = new foo("Ted");
ObjOne.CreateElement();

var ObjTwo = new foo("Steve");
ObjTwo.CreateElement();

and when the onclick fires for ObjOne, I get the object instance for ObjTwo and so this.Name is set to Steve.

Does anyone know how I can get the right object instance when the onclick event fires from various instances? I assume i need to bind the correct instance with call, but i’m not really sure how to.

Thanks in advance and hope this wasn’t to confusing.

  • 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-30T05:05:00+00:00Added an answer on May 30, 2026 at 5:05 am

    The problem is this line:

    that = this;
    

    You declared that without using var, which means it is global, i.e., all your instances are updating the same global that variable. Do this instead:

    var that = this;
    

    Then that will be local to each. (That is, local to each closure of the CreateElement function.)

    Demo: http://jsfiddle.net/SEsLJ/

    Also, document.getElementsByName should be document.getElementsByTagName (assuming you didn’t actually give the body a name attribute equal to “body”).

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

Sidebar

Related Questions

I have created a jQuery plugin that works great with the exception of being
I have an object created in a host application and can access it remotely
I've created a object that I'd like to have accessible from wherever the request-object
I have tree control object created using CTreeCtrl MFC class. The tree control needs
I have a stdClass object created from json_decode that won't return the right number
I have an app that uses EF POCO for accessing data. Everything works great,
I have two sites which I use the same cookie for login which works
I'm reusing a created Object just to change a Date and the ordinal value,
I would like to return the id of a newly created object for my
Is it bad javascript practice to not assign a newly created object to a

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.