Onclick event to remove default value in a text input field

前端 未结 9 1297
傲寒
傲寒 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 14:57

    HTML5 Placeholder Attribute

    You are likely wanting placeholder functionality:

    <input name="Name" placeholder="Enter Your Name" />
    

    Polyfill for Older Browsers

    This will not work in some older browsers, but polyfills exist (some require jQuery, others don't) to patch that functionality.

    "Screw it, I'll do it myself."

    If you wanted to roll your own solution, you could use the onfocus and onblur events of your element to determine what its value should be:

    <input name="Name" value="Enter Your Name"
           onfocus="(this.value == 'Enter Your Name') && (this.value = '')"
           onblur="(this.value == '') && (this.value = 'Enter Your Name')" />
    

    Avoid Mixing HTML with JavaScript

    You'll find that most of us aren't big fans of evaluating JavaScript from attributes like onblur and onfocus. Instead, it's more commonly encouraged to bind this logic up purely with JavaScript. Granted, it's a bit more verbose, but it keeps a nice separation between your logic and your markup:

    var nameElement = document.forms.myForm.Name;
    
    function nameFocus( e ) {
      var element = e.target || window.event.srcElement;
      if (element.value == "Enter Your Name") element.value = "";
    }
    
    function nameBlur( e ) {
      var element = e.target || window.event.srcElement;
      if (element.value == "") element.value = "Enter Your Name";
    }
    
    if ( nameElement.addEventListener ) {
      nameElement.addEventListener("focus", nameFocus, false);
      nameElement.addEventListener("blur", nameBlur, false);
    } else if ( nameElement.attachEvent ) {
      nameElement.attachEvent("onfocus", nameFocus);
      nameElement.attachEvent("onblur", nameBlur);
    }
    

    Demo: http://jsbin.com/azehum/2/edit

    0 讨论(0)
  • 2021-02-02 14:59

    You actually want to show a placeholder, HTML 5 offer this feature and it's very sweet !

    Try this out :

    <input name="Name" placeholder="Enter Your Name">
    
    0 讨论(0)
  • 2021-02-02 15:00
    <input name="Name" value="Enter Your Name" onfocus="freez(this)" onblur="freez(this)">
    
    
    function freez(obj)
    {
     if(obj.value=='')
     {
       obj.value='Enter Your Name';
     }else if(obj.value=='Enter Your Name')
     {
       obj.value='';
     }
    } 
    
    0 讨论(0)
  • 2021-02-02 15:03

    This is the right, cross-browser way to do it :

    <input type="text" value="Enter Your Name" onfocus="if(this.value  == 'Enter Your Name') { this.value = ''; } " onblur="if(this.value == '') { this.value = 'Enter Your Name'; } " />
    
    0 讨论(0)
  • 2021-02-02 15:06

    u can use placeholder and when u write a text on the search box placeholder will hidden. Thanks

    <input placeholder="Search" type="text" />
    
    0 讨论(0)
  • 2021-02-02 15:07

    Use onclick="this.value=''":

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