Jak Ju Jak Ju - 1 year ago 81
Javascript Question

How do I use blur in jQuery?

I have the following code:

$(function() {
$('.type_choice_textarea').on('focus', function() {
$(this).css("height", "150px");
});
if ($('.type_choice_textarea').val().length == 0) {
$('.type_choice_textarea').on('blur', function() {
$(this).css("height", "30px");
});
}
});


but
blur
does not work. How do I use
if
in
blur
?

Answer Source

Just write you if condition within blur event handler

 $(function() {
    let elem = '.type_choice_textarea';

    $(elem).on('focus', function() {
        $(this).css("height", "150px");
    });

    $(elem).on('blur', function() {
      if ($(this).val().length == 0) {            
        $(this).css("height", "30px");
      }
    });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download