JQuery add percent sign to input field

I would like to have an input box that automatically adds a visible percent sign to the user when entering numbers (not just recognizing it as a percentage when sending). Thus, the user presses "2" and sees "2%"

I guess jQuery can use this quite easily, but I have no idea how to do this! Any ideas?

Thanks to everyone.

+7
jquery
source share
2 answers

You can handle the change event:

 $(':input.Percent').change(function() { $(this).val(function(index, old) { return old.replace(/[^0-9]/g, '') + '%'; }); }); 
+11
source share

In Keyup event

 $('input').keyup(function(e) { if(e.which != 13) { //13 is enter, you dont want to submit the form on enter var value = $.trim($(this).val()); if(value != '') { $(this).val(value +'%'); } } else { return false; } }); 
-one
source share

All Articles