/    Sign up×
Community /Pin to ProfileBookmark

How do I call Javascript function in a class from HTML onclick event?

function Calc() {
alert(‘Hi class’);
var Sum = function() { alert(‘Hi function’); };
};

//<button onclick=”new Calc().Sum”> ClickMe </button>

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 19.2014 — I'm not exactly sure what you are trying to do here, so this is just a SWAG...
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;

&lt;title&gt; HTML5 page &lt;/title&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;button onclick="Calc()"&gt; ClickMe &lt;/button&gt;

&lt;script type="text/javascript"&gt;
function Calc() {
alert('Hi class');
var Sum = function() { alert('Hi function'); };
Sum();
};
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@toicontienJun 20.2014 — He wants to combine object oriented JavaScript with event handling. Three basic ways come to mind.

[B]Outside code binds the event handlers[/B]

[code=html]<form ...>
...

<button type="button" id="calculateButton">Click Me</button>
</form>
[/code]

<i>
</i>function Calc() {
}
Calc.prototype = {
constructor: Calc,

<i> </i>sum: function(event) {
<i> </i> event.preventDefault();
<i> </i> var form = event.target.form;
<i> </i> // do some calculation
<i> </i>}
};

var calculator = new Calc();

document.getElementById("calculateButton").addEventListener("click", function(event) {
calculator.sum(event);
}, false);


[B]The class binds event handlers[/B]

[code=html]
<form id="calculator">
...

<button type="button" class="calculateButton">Click Me</button>
</form>

<script type="text/javascript">
function Calc(form) {
this.form = typeof form === "string"
? document.getElementById(form)
: form;

this.sum = this.sum.bind(this);
this.form.querySelector("button.calculateButton").addEventListener("click", this.sum, false);
}
Calc.prototype = {
form: null,

constructor: Calc,

sum: function(event) {
event.preventDefault();

// use this.form to do some calculation...
}
};

var calculator = new Calc("calculator");
</script>
[/code]


[B]Use an event delegation library[/B]

I created [url=https://github.com/gburghardt/oxydizr]Oxydizr[/url] as an easy way to bridge event delegation and object oriented code.

[code=html]
<form id="calculator">
...

<button type="button" data-action="calculator.sum">Click Me</button>
</form>

<script type="text/javascript" src="path/to/Oxydizr.js"></script>
<script type="text/javascript" src="path/to/Oxydizr/FrontController.js"></script>
<script type="text/javascript">

// Your JavaScript "class"
function Calc(form) {
this.form = typeof form === "string"
? document.getElementById(form)
: form;

this.controllerId = this.form.id;
}
Calc.prototype = {
form: null,

constructor: Calc,

onControllerRegistered: function(frontController, controllerId) {
// listen to additional events, but the front controller gives you
// click and submit event handlers for free.
},

onControllerUnregistered: function(frontController) {

},

// An "action handler" that handles a "click" event
sum: function click(event, element, params) {
event.preventDefault();

// use this.form to do some calculation...
// element is <button data-action="calculator.sum">...</button>
// params is an empty object
}
};

// Create the front controller
var frontController = new Oxydizr.FrontController()
.init(document.documentElement);

// Create your calculator controller
var calculator = new Calc("calculator");

// Register your calculator controller with the front controller to wire up event handling
frontController.registerController(calculator);
</script>
[/code]
×

Success!

Help @Bipul spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...