How to set an input width to match the placeholder text width

前端 未结 4 1293
梦毁少年i
梦毁少年i 2020-12-08 18:39

Example http://dabblet.com/gist/5859946

If you have a long placeholder, the input by default does not display wide enough to show all the placeholder text.

e

4条回答
  •  孤街浪徒
    2020-12-08 19:02

    Just in case someone wants to use this with jQuery, that code would be below. Also, if the placeholder attribute doesn't exist in the accepted answer, you'll get errors, which is taken care of as well in the jQuery example below.

    $("input[placeholder]").each(function () {
            $(this).attr('size', $(this).attr('placeholder').length);
        });
    

提交回复
热议问题