Reputation: 6820
Hi I need a jQuery math plugin or a way to do the following
say I have $100 and I get $10 back I need it to automatically show me on the screen 5% or 0.05
If anyone knows how to do this, I would be greatful.
Upvotes: 0
Views: 2430
Reputation: 14737
A math plugin? That sounds like you're over-complicating the solution.
What's stopping you from just creating a simple function in plain old Javascript?
function GetPercentage(whole, part) {
return (part / whole);
}
... and even that is over-complicating it I think. What exactly is your use case?
Oh, and $10 from $100 is 10%, not 5%. :D
Upvotes: 2
Reputation: 6181
You can bind to the change event on the various fields.
For example, given the following HTML:
<input type="text" id="Amount"/><input type="text" id="GotBack"/><input type="text" disabled="disabled" id="Percent"/>
You can use the following jQuery to bind to the respective field's change events:
$("#Amount, #GotBack").change(function() {
$("#Percent").val(parseFloat($("#GotBack").val())/parseFloat($("#Amount").val()));
});
Bear in mind this doesn't do any checks for the validity of the fields, nor does it format the text prettily, but I'll leave this as an exercise to the reader. :)
Upvotes: 0