padders01
padders01

Reputation: 29

Calculate percentage .asp

I am looking to build a webpage that submits into a database (.asp) but also calculates a percentage.

The percentage amount is 8%.

Ideally I want the user to type an amount in an input box (for example lets say £100)

Then underneath there is another input box which automatically caluates the £100 + 8% (so £108)

I haven't got a clue how to start so I haven't got any code

Upvotes: 0

Views: 441

Answers (2)

JP Hellemons
JP Hellemons

Reputation: 6057

Live jsFiddle: http://jsfiddle.net/5mB3Q/

This is your jQuery:

$("#val").on('keyup', function(){
    $("#incPerc").text(parseInt($(this).val())*1.08);
});

you might want to use a library to limit the input to digits only. something like this: Limit Textfield Input to digits only

added the second input http://jsfiddle.net/5mB3Q/1/ and the round of two digits:

$("#val").on('keyup', function(){
    var withPerc = parseFloat($(this).val())*1.08;
    $("#incPerc").text(withPerc.toFixed(2));
    $("#otherInput").val(withPerc.toFixed(2));
});

edit: ok, full code because of the comment:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script src="http://code.jquery.com/jquery-2.0.0.js" type="text/javascript"></script>
  </head>
  <body>
    <form>
      <input id="val">
      <input id="otherInput">
    </form>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#val").on('keyup', function(){ 
                var withPerc = parseFloat($(this).val())*0.08; 
                $("#incPerc").text(withPerc.toFixed(2)); 
                $("#otherInput").val(withPerc.toFixed(2)); 
            });
        });
    </script>
  </body>
</html>

You forgot the protocol when you linked the jQuery lib. (and doc.ready)

Upvotes: 1

Gintas K
Gintas K

Reputation: 1478

$('#output').text = (int)($('#input').text.Substring(0, $('#input').text.Length - 1)) * 1,08 + $('#input').text.Substring($('#input').text.Length - 1);

Upvotes: 0

Related Questions