JQuery input mask not working

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() { //This is one of the masking codes I attempted. $('.toTotal').mask('9.99', {reverse: true}); //other options I have tried: //$('.toTotal').mask('9.99'); //$('.toTotal').mask('0.00'); //$('.toTotal').inputmask('9.99'); //$('.toTotal').inputmask('mask', {'mask': '9.99'}); var calcTot = function() { var sum = 0; $('.toTotal').each( function(){ sum += Number( $(this).val() ); }); $('#giveTotal').val( '$' + sum.toFixed(2) ); } calcTot(); $('.toTotal').change( function(){ calcTot(); }); //I have tried putting it here, too }); </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> 
+6
source share
1 answer

There is no masking script library specified in the sample code. You need to download the digital plug-in Script input plugin and copy it to the JS folder.

Then add the following script link after the line `jquery.js':

 <script src="/js/jquery.maskedinput.min.js"></script> 
+7
source

Source: https://habr.com/ru/post/949130/


All Articles