I am trying to create a donation page for people to give money to a nonprofit organization and let them indicate the use of money. I found that he sums up the amounts that the giver puts in each field when they enter the amounts. I am trying to add an input mask to each field, but this just causes JavaScript to crash and does nothing. Here is the code I have that works fine in front of any masks:
<script src="/js/jquery.js"></script> <script type="text/javascript"> $(document).ready( function() { var calcTot = function() { var sum = 0; $('.toTotal').each( function(){ sum += Number( $(this).val() ); }); $('#giveTotal').val( '$' + sum.toFixed(2) ); } calcTot(); $('.toTotal').change( function(){ calcTot(); }); }); </script>
'toTotal' is the class name specified for all input fields that need to be added; this is also a class that needs a mask. 'giveTotal' is the identifier of the full field.
I tried several options that I found on StackOverflow and other sites.
Full code:
<html> <head> <script src="/js/jquery.js"></script> <script type="text/javascript"> $(document).ready( function() { </script> <title>Addition</title> </head> <body> <input type="text" class="toTotal"><br /> <input type="text" class="toTotal"><br /> <input type="text" class="toTotal"><br /> <input type="text" id="giveTotal"> </body> </html>
source share