Onclick event to remove default value in a text input field

前端 未结 9 1258
傲寒
傲寒 2021-02-02 14:57

I have an input field:


How would I get it to remove the pre-defined text (Enter Your Name

相关标签:
9条回答
  • 2021-02-02 15:08

    As stated before I even saw this question placeholder is the answer. HTML5 for the win! But for those poor unfortunate souls that cannot rely on that functionality take a look at the jquery plugin as an augmentation as well. HTML5 Placeholder jQuery Plugin

    <input name="Name" placeholder="Enter Your Name">
    
    0 讨论(0)
  • 2021-02-02 15:08

    This should do it:

    HTML

    <input name="Name" value="Enter Your Name" onClick="blankDefault('Enter Your Name', this)">
    

    JavaScript

    function blankDefault(_text, _this) {
        if(_text == _this.value)
            _this.value = '';
    }
    

    There are better/less obtrusive ways though, but this will get the job done.

    0 讨论(0)
  • 2021-02-02 15:20

    In addition to placeholder="your text" you could also do onclick="this.value='';

    So it would look something like:

    <input name="Name" value="Enter Your Name" onclick="this.value='';>
    
    0 讨论(0)
提交回复
热议问题