cdr6800
cdr6800

Reputation: 105

Textbox populated by jquery function needs decimals

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?

Upvotes: 2

Views: 33

Answers (2)

guest271314
guest271314

Reputation: 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");

});

Upvotes: 1

Chris
Chris

Reputation: 4810

You should check out toFixed()

$('textbox').on('change', function(evt) {
    var val = $(this).val();
    var wDecimal = val.toFixed(2);
});

Upvotes: 0

Related Questions