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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:44:59+00:00 2026-06-04T01:44:59+00:00

I have a class Foo, with a method that generates HTML that is displayed.

  • 0

I have a class Foo, with a method that generates HTML that is displayed. I want the HTML to have an onclick event handler that calls Foo.clickHandler. The problem is that I don’t know what this particular instance of Foo is named. Likewise, the onclick event has no way of knowing how to access this instance of Foo. Here is some code:

function Foo(){
    this.nonStaticVariable='Something non-static (different for every instance of Foo).';
    this.getHTML=function(){
        return '<a href="javascript:void(0);" onclick="/* How do I call Foo.clickHandler? */">Click Me!</a>';
    }
    this.clickHandler=function(){
        alert(nonStaticVariable);
    }
}

The point of the non-static function is to show that the onclick needs to call the correct instance of Foo.

I have thought about passing a string to Foo that contains the variable name that contains Foo, but this seems anti-OOP:

function Foo(container){
    this.container=container;
    this.nonStaticVariable='Something non-static (different for every instance of Foo).';
    this.getHTML=function(){
        return '<a href="javascript:void(0);" onclick="'+container+'.clickHandler();">Click Me!</a>';
    }
    this.clickHandler=function(){
        alert(nonStaticVariable);
    }
}

var fooInstance=new Foo('fooInstance');

What do you suggest?

I am open to jQuery solutions as well.

  • 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-04T01:45:01+00:00Added an answer on June 4, 2026 at 1:45 am

    Do nonStaticVariable and clickHandler need to be accessible outside of Foo? If not, you could simply do something like this:

    function Foo(){
        //changed these to private variables only accessible from within Foo
        var nonStaticVariable='Something non-static (different for every instance of Foo).';
        var clickHandler = function(){
            alert(nonStaticVariable);
        }
        this.getHTML=function(){
            return $('<a href="#">Click Me!</a>').click(clickHandler);
        }
    }
    
    
    var fooInstance = new Foo();
    
    var button = fooInstance.getHTML();
    
    
    $("#container").html(button);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have the follow class. class Foo # I want the following
Say I have an abstract base class that has a method to generate Foo
I have a controller action that calls a model method which generates a serialized
I have a method in a class as follows... class foo{ int bar::randomNum10to50(){ srand
If I have for example a class with instance method and variables class Foo
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
I have a base class foo that will be used in multiple child classes
I have a base class Foo that is concrete and contains 30 methods which
I have a class Foo that a property of the type Bar. public class
Say I have class Foo and I have a FooRepository class with a method

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.