Toggle a money symbol to a form field on focus/blur - useful for cost data


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. jQuery.fn.moneyField = function(symbol) {
  2. return this.each(function() {
  3. $(this).focus(function(){
  4. if(!$(this).val()) $(this).attr('value', symbol);
  5. }).blur(function(){
  6. if($(this).val() == symbol) {
  7. $(this).attr('value', '');
  8. } else {
  9. s = $(this).val();
  10. s = $.numAddCommas(s); // Add commas to long digit strings - you will need to get a function to do this (find one on snipplr)
  11. $(this).attr('value', s);
  12. var hascur = s.indexOf(symbol);
  13. if(hascur) $(this).attr('value', symbol+s);
  14. }
  15. }).prev('label').click(function(){
  16. return false;
  17. });
  18. });
  19. }
  20.  
  21. $(function(){
  22. $('input[name=money_field]').moneyField('£');
  23. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.