Empty default text When TextBox gets focus in ASP.

Source: Internet
Author: User

When the textbox in ASP. NET clears the default text, keyword search and other prompt content input:

Method One:

    <script type= "Text/javascript" >     function cls () {            var obj = document.getElementById (' Txtkeyword ');             Obj.style.color = ' #000 ';            With (event.srcelement)                if (value = = DefaultValue) value = ""            }    function res () {                var obj = Document.getele Mentbyid (' Txtkeyword ');                Obj.style.color = ' Gray ';                With (event.srcelement)                    if (value = = "") value = DefaultValue        }    </script>

        <Asp:textboxId= "Txtkeyword"Runat= "Server"Text= "Please enter keywords"Style= "width:80%; Color:gray "
CssClass= "Inputcss"onfocus= "CLS ()"Onblur= "Res ()">
</Asp:textbox>
If the page contains a master page, the textbox above has no effect. You can delete the above script to change the control directly to:
<asp:textbox id= " Txtkeyword " Runat=" Server " Text= "Enter the keyword" Style= "width:80%; Color:gray "
Cssclass=" inputcss "  onfocus=" if (value = = DefaultValue) {value= "; this.style.color= ' #000 '}"
Onblur= "if (! Value) {value = Defaultvalue;this.style.color= ' #999 '} "

</< Span style= "COLOR: #800000" >asp:textbox>

Method Two: "Turn"

function is, a text box, need to enter the content, there is no input when there is a hint of content, when clicked on this text box input, the contents of the text box automatically disappears. Quite simple a function, but did not think of the implementation is still very troublesome, on the internet to find a piece of code, affixed to prepare for later use.

Add trigger Events onfocus and onblur to the text box, calling function processing. Full code:

<script>
Function cls () {
With (event.srcelement)//captures the object that triggered the event and sets the default object to the following statement
if (value==defaultvalue) value= ""//if the current value is the default value, clear the
}
function res () {
With (event.srcelement)//captures the object that triggered the event and sets the default object to the following statement
if (value== "") Value=defaultvalue//reset to default value if current value is empty
}
</script>
<input value= "Fill in your nickname" onfocus= "CLS ()" Onblur= "res ()"/>
Note: The method in this example is valid for most form controls, such as a multiline text box.

Special tip: Run the full code, the text box content is emptied when the mouse first clicks the text box, and the text box content reverts back to the default value when clicked outside the text box. If you change the value of the text box, there will be no change.

============================================================================================

Or "more like this, simple and practical"

<input type= "text" id= "text" name= "text" value= "default display text" Onfocus= "if (value = = defaultvalue) {value= '; This.style.color= ' #000 '} "onblur=" if (!value) {value = Defaultvalue;this.style.color= ' #999 '} "style=" width:200px; Color: #999999; "/>

Empty default text When TextBox gets focus in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.