Make input field background image disappear after text is inputted

前端 未结 4 595
北荒
北荒 2021-01-24 07:59

I\'d like to make the background image for my input field disappear once the user has typed any amount of text in it. Is there a simple way to do that in javascript? I can get i

4条回答
  •  长发绾君心
    2021-01-24 08:02

    Or for maximum browser compatibility (ahem, IE):

    $("input.phone-field").change(function() { $.trim($(this).val()) != "" ? $(this).toggleClass("bg", false) : $(this).toggleClass("bg"); });
    

    And the markup:

    
    

    Just add the background image CSS code to the ".phone-field.bg" selector.

    This would remove the background as soon as some text is entered (and bring it back if no text is entered).

提交回复
热议问题