2

in a previous post, I received help with a jquery function that populates a textbox with a dropdown selection. I needed to use it to populate another textbox based on the same selection.

However, I need this textbox to have 2 decimal points and keep its value as a number because I will be using it in a calculation. Here is the code that populates the textbox:

<script>
    $(function () {
        $("#RaffleDollars").change(function () {
            if ($(this).val() == "25")
                $("#Total_Raffle").val(25);
        });
    });
</script>

How can I get the value of Total_Raffle to display as 25.00 while keeping its format as a number used in a calculation?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
cdr6800
  • 105
  • 2
  • 9
  • duplicate of http://stackoverflow.com/questions/9881773/converting-a-value-to-2-decimal-places-within-jquery – shemy Oct 27 '15 at 02:48

2 Answers2

1

while keeping its format as a number used in a calculation?

input element value is text , could convert to number object by casting with Number(value)

$("#RaffleDollars").change(function () {
 if ($(this).val() == "25")
   $("#Total_Raffle").val(25 + ".00");

});
guest271314
  • 1
  • 15
  • 104
  • 177
0

You should check out toFixed()

$('textbox').on('change', function(evt) {
    var val = $(this).val();
    var wDecimal = val.toFixed(2);
});
Chris
  • 4,762
  • 3
  • 44
  • 79
  • Chris, thanks for your answer. I know about .toFixed(2); but I guess I am not sure how to execute it in my code. I tried this but it did not work: $("#Total_Raffle").val(25).toFixed(2); It still appeared in the textbox as 25. Any idea what I am doing wrong? Thanks. – cdr6800 Oct 27 '15 at 01:18
  • `toFixed()` is applied to the value first. You need to run it before updating the value. `var amt = 25.toFixed(2); $('#Total_Raffle').val(amt);` – Chris Oct 27 '15 at 01:20