What I have at the moment is a script that out puts a value after adding together different input fields.
<script type="text/javascript">
$(function() {
$("#addAll").click(function() {
var add = 0;
$(".amt").each(function() {
add += Number($(this).val());
});
$("#para").html("$<input size="18" name="salestax" class="amt" readonly type="text" value=" + add + " />");
});
});
</script>
I am trying to limit the decimal place of this output to only 2 decimal places.
What do I need to add? I assume its the toFixed() but I have tried that in numerous places. Where should it be placed?
You should always avoid reducing the precision of your floating point values until as late as possible. In your case, you want to apply
toFixed()in the line that constructs the display HTML. Also, you are missing quote marks around the value attribute. Your code should look something like this:I’m not sure that
"is really necessary, I think you could just use apostrophes instead (').