Clear input on focus with jQuery and return on blur

后端 未结 12 2309
耶瑟儿~
耶瑟儿~ 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:33

    Try this in your firebug:

    $(function() { console.log($(this)) });
    

    You will find out that

    var defaultText = $(this).val();
    

    is probably not what you want.

    If initial value for the input is the default text, obtain it like this:

    var defaultText = $('input[type=text]').val()
    

    Be aware, that this will work correctly only if there is just one input text on your page.

    And also remove quotes in:

    $(this).val('defaultText');
    

提交回复
热议问题