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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:20:19+00:00 2026-06-04T04:20:19+00:00

How do I execute a JS object’s function property from an HTML link? I

  • 0

How do I execute a JS object’s function property from an HTML link?
I have the following JS:

function Tester(elem) {
    this.elem = document.getElementById(elem);
}

Tester.prototype.show = function() {
    this.elem.innerHTML = '<a href="javascript: this.test();">test</a>';
};

Tester.prototype.test = function() {
    alert("a");
};  
​

Here is the HTML:

<script type="text/javascript">
    var test = new Tester("test");
    test.show();
</script>

When I click on the link that gets rendered, it cannot identify the test() function. How would I get it so when a user clicks on the link, the test() function is executed?

  • 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-04T04:20:20+00:00Added an answer on June 4, 2026 at 4:20 am

    The proper way would be to create a DOM element and attach the event handler with JavaScript:

    Tester.prototype.show = function() {
        var a = document.createElement('a'),
            self = this; // assign this to a variable we can access in the 
                         // event handler
    
        a.href = '#';
        a.innerHTML = 'test';
        a.onclick = function() {     
            self.test();
            return false; // to prevent the browser following the link
        };
    
        this.elem.appendChild(a);
    };
    

    Since the event handler forms a closure, it has access to the variables defined in the outer function (Tester.prototype.show). Note that inside the event handler, this does not refer to your instance, but to the element the handler is bound to (in this case a). MDN has a good description of this.

    quirksmode.org has some great articles about event handling, the various ways you can bind event handlers, their advantages and disadvantages, differences in browsers and how this behaves in event handlers.

    It’s also certainly helpful to make yourself familiar with the DOM interface.

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

Sidebar

Related Questions

I have just started learning MVVM. I've made the application from scratch by following
Is it possible to execute an SPDataSource object query in a console app for
I'm starting a little job manager, it will execute some Job object implement an
I want to execute a function on all the objects within a List of
I execute sql queries as transactions using jdbi inTransaction() function. I would like to
Here's the issue. I have a prepared statement, like this: $select_something = $db->stmt_init(); $select_something->prepare
This is such a basic question, but I have to ask. In SL, I
I'm using Devart to execute DB object and Store Procedure mapping. Now i want
How do I execute QueryCommand Object in SubSonic 3.0.0.4 without DataService?
I want to make a method: object Execute() { return type.InvokeMember(..); } to accept

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.