Calculator working out percentages


If you go to this page
and enter a donation amount and then click on the yes box you will see the percentage of the gift aid is worked out.

Can anyone help with a JS way to do this please?

Hi there,

This should get you started:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Percentage calculator</title>
      <label for="amount">Amount £</label>
      <input id="amount" type="text" />
      <span id="total"></span>
      <label for="addPercentage">Add percentage</label>
      <input type="checkbox" id="addPercentage">
      var percentage = document.getElementById("addPercentage"),
          amount = document.getElementById("amount"),
          total = document.getElementById("total");
      percentage.onchange = function(){
          if (amount.value && amount.value.match(/^(\\d|,)*\\.?\\d*$/)){
            total.innerHTML = "Value plus 25% is £" + (Number(amount.value) * 1.25).toFixed(2);
        } else {
          total.innerHTML = "";

When you check the checkbox, presuming that there is a value entered in the amount field, the script tests the amount against a reg ex to verify that it is a valid number (matches numeric with commas and a single decimal point), then calculates the percentage accordingly.

It is by no means perfect, but should get you thinking in the right direction.
If you have any questions, just let me know.