You need to use a delegated handler, since the input does not have an input2 class when using the handler.
$(document).on('focus','input.input1, textarea.input1', function() { $(this).addClass('input2').removeClass('input1'); }); $(document).on('blur','input.input2, textarea.input2', function() { $(this).addClass('input1').removeClass('input2'); });
However, there are probably better ways to do this. I would suggest using a third class to indicate inputs that need a class that switches, and then just switch classes when an event occurs.
$('.needsToggle').on('focus blur', function() { $(this).toggleClass('input1 input2'); });
If they always have an input1 class to run, you can use it instead of needsToggle .
source share