Apart from your specific example using rates: here's a general description that just shows how to make a square-root calculator. You can extend it to meet your needs. In general, you'll need to follow these steps:
- Include jquery.js in some HTML page that renders your calculator
- Have
<input type="text" id="mynum"/>
fields in your HTML where users input values (or usetype="number"
etc.). Make sure you useid="...."
as the field ID, you'll re-use it later. - Have a placeholder to show results, e.g.
<div id="result">Calculation result will appear here.</div>
. Again,id="...."
is used to identify this element so that you can put in values using Javascript. - Have some buttons to do calculations, e.g.
<input type="button" value="SQRT" onclick="sqrt_stuff();"/>
Next comes the calculation part. The hypothetical above function sqrt_stuff()
would then use JQuery to pick up values and paste results; for example:
function sqrt_stuff() {
// Get user input from field with id "mynum"
var nr = $('#mynum');
// Calculate root
var root = Math.sqrt(nr);
// Show in div with id "result"
$('#result').html('The answer is: ' + root);
}