Please tell me how to fix (or correct) the code so that when the focus is lost, the added class is removed from the element.

$(".flat-field" ) .focus( function() { $( this ).addClass('flat-field-focus'); } ) 

I tried using mouseleave, but it partially solved my problem.

    2 answers 2

    You will help event blur

     $(".flat-field" ) .blur( function() { $( this ).removeClass('flat-field-focus'); } ) 

      Two in one:

       $('.flat-field').on('focus blur', function (evt) { $(this).toggleClass('flat-field-focus', evt.type === 'focus'); });