简体   繁体   中英

Image set in text box just like placeholder in js?

I want to be able to hide a certain image while having a textbox filled in and when the textbox is cleared I want to show a certain image. I know it should work something like this but with image instead of placeholder:

$('input:text').focus(function () {
    if ($(this).val() === $(this).attr('placeholder')) {
        $(this).val('');
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val($(this).attr('placeholder'))
    }
});

Thanks in advance for your kind answers.

Try -

$('input:text').focus(function () {
    $(this).css('background', 'none');

}).blur(function() {
    if ($(this).val() == "") {
        $(this).css('background', 'url(http://profile.ak.fbcdn.net/hprofile-ak-prn1/71158_111108655596607_6920099_n.jpg) no-repeat left top');
    }
});

Check Demo

With backspace key -

Updated Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM