-1

I have the following jquery function that auto populates a textbox based on the selection of a radio button option:

<script>
    $(document).ready(function() {
        $("#DinnerYes").click(function() {
            $("#ThursdayDinnerFee").attr("value", 90);
        });
    });
    $(document).ready(function() {
        $("#DinnerNo").click(function() {
            $("#ThursdayDinnerFee").attr("value", 0);
        });
    });
</script>

The script works and it auto populates the textbox (ThursdayDinnerFee), but I want the numbers to appear in the textbox as 0.00 and 90.00. I also need to keep the format as number because they will be used in a credit card transaction. If someone could help, I would greatly appreciate it. Thank you.

Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
dr1054
  • 89
  • 1
  • 4
  • 10
  • please try searching before asking. Even on right side of this page under **Related** there are several answers you could have looked at – charlietfl Sep 27 '16 at 19:03

3 Answers3

1

Use toFixed() method like following.

$("#ThursdayDinnerFee").val((90).toFixed(2))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="ThursdayDinnerFee" type="text">
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
0

This can be done with pure JS (no jQuery needed):

Please take a look at the toFixed() method http://www.w3schools.com/jsref/jsref_tofixed.asp

meavo
  • 1,042
  • 1
  • 7
  • 16
0

To keep the format you could use the HTML5 Pattern-Tag.

0xDECAFBAD
  • 627
  • 1
  • 8
  • 21