Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery add percent sign to input box

Tags:

jquery

I'd like to have an input box that automatically adds a visible percent sign to the user when entering numbers (not just recognizes it as a percent when submitting). Thus, a user hits "2" and sees "2%"

I'm assuming one could use Jquery to do this fairly easily, but I have no idea how! Any ideas?

Thanks everyone.

like image 888
kingzing1 Avatar asked Apr 20 '10 19:04

kingzing1


1 Answers

You can handle the change event:

$(':input.Percent').change(function() {
    $(this).val(function(index, old) { return old.replace(/[^0-9]/g, '') + '%'; });
});
like image 96
SLaks Avatar answered Oct 18 '22 08:10

SLaks