Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 2
class methods... ehh
Dagg Nabbit
  • 77k
  • 19
  • 115
  • 142

Calling JavaScript functions from HTML

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?

egidra
  • 9.1k
  • 21
  • 68
  • 92
lang-js

AltStyle によって変換されたページ (->オリジナル) /