Search code examples
jquerycallbackjquery-callback

JQuery: attach the same callback to one or more elements


I have to prevent user to insert letters in numeric fields. I use this:

 $('input[name=myinput]').live('keyup', function() {
                var $th = $(this);
                $th.val($th.val().replace(/[^0-9,\.]/g, ''));
        });

How could I list the other input fields? (I can't use $('input'))

I tried with

$('input[name=myinput],input[name=myinput2]').live('keyup', function() {
                var $th = $(this);
                $th.val($th.val().replace(/[^0-9,\.]/g, ''));
        });

but only input[name=myinput] is accepted.


Solution

  • You should use a class on the input element and then do:

    $('input.eKeyup').live('keyup', function() {
        var $th = $(this);
        $th.val($th.val().replace(/[^0-9,\.]/g, ''));
    });