Clear input on focus with jQuery and return on blur

后端 未结 12 2304
耶瑟儿~
耶瑟儿~ 2021-02-09 17:50

This almost works. However, when leaving the field \"defaulttext\" appears rather than the original text value. Not sure how to most efficiently echo the variable inside default

12条回答
  •  北海茫月
    2021-02-09 18:42

    You're not referencing the variable defaultText. You're passing a string since it's wrapped in quotes.

    $(function() {
        var defaultText = $('input[type=text]').val();
        $('input[type=text]').focus(function() {
          $(this).val('');
          });
         $('input[type=text]').blur(function() {
          $(this).val(defaultText); // fixed!
          });
     });
    

提交回复
热议问题