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

Return to Question

class methods... ehh
Source Link
Dagg Nabbit
  • 77k
  • 19
  • 115
  • 142

Calling on JS class methodsJavaScript functions from HTML

How do I execute a JS object's class methodfunction 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() methodtest() function. How would I get it so when a user clicks on the link, the test() methodtest() function is executed?

Calling on JS class methods from HTML

How do I execute a JS object's class method 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() method. How would I get it so when a user clicks on the link, the test() method is executed?

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?

Source Link
egidra
  • 9.1k
  • 21
  • 68
  • 92

Calling on JS class methods from HTML

How do I execute a JS object's class method 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() method. How would I get it so when a user clicks on the link, the test() method is executed?

lang-js

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