Click the text box to clear the default text left and then restore

Source: Internet
Author: User

Click the text box to clear the default text to leave and then restore:
Many web sites need to fill in the text box in the default state will give a default prompt language, when the mouse click on this text box can be the default text inside clear, when the input text is deleted and the focus away from the text box when the default text is written to the text box.
The code is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Click the text box to clear the default values-ant tribe</title><Scripttype= "Text/javascript">window.onload=function(){  varusername=document.getElementById ("username"); Username.onclick=function()  {    if(Username.value=="Please enter your name") {Username.value="";  This. focus (); }} Username.onblur=function()  {    if(Username.value=="") {Username.value="Please enter your name"; }  }}</Script></Head><Body><inputtype= "text"value= "Please enter your name"ID= "username" /></Body></HTML>

The above code to achieve our requirements, when clicking on the text box can clear the text box content, if the text box does not enter any content, this time when the mouse focus away from the text box, the value of the text box will be restored to the default state. However, if the password box Ken is a bit troublesome because the password box is not displayed in clear text, the solution can see How to Implement the Password box in the prompt section.
Related reading:
1.onclick events can be found in the OnClick events section of JavaScript .
The 2.onblur event can be found in the Onblur event section of JavaScript .

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9421

For more information, refer to: http://www.softwhy.com/javascript/

Click the text box to clear the default text left and then restore

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.