Hide/Show text when input field gets/loses focus (jquery version)

Source: Internet
Author: User

The input box hides or displays text when it Gets or loses focus, so the focus effect is that many friends have seen it when filling out form forms, this article uses jquery to achieve the following, interested friends can refer to ha

Everyone can look at the search input box, the default display "user name/email" prompt, when the input box to get the focus, automatically empty waiting for user input, when the user did not enter anything to leave the input box, the input box again displays "User name/email" prompt. Isn't it common? Many search, login, form will use this effect, but I see more than n sites, more than 90% is so implemented:

<input type="text" value=" search keyword " onfocus="  if (this.value = = ' search keyword ') this.value = '"" onblur="if (this.value = =") this.value = ' Search keyword ' /c10>"

I am very opposed to writing JavaScript in HTML tags, and this style is written in the HTML tag, although it does not violate the standards, but it is not recommended to write. Because:
1. There is no reusability at all, if it is a form, a lot of input boxes, each need this effect, then each of them so deal with it?
2. If you want to modify the hint text, it is laborious and difficult to maintain.
3. We advocate the separation of structure (HTML), performance (CSS), and Behavior (JavaScript), which is a good page.
How to write to achieve this effect, and both reusability, and good maintenance, and do not need to put JS into the HTML it?
Here's how:
The first must be the introduction of jquery
HTML code:

<div><input type="text" value=" hint test "class ="input_test" /></div> <div><input type="text  " value=" Please enter the search key "class="input_test "

jquery Code:

<script type="Text/javascript"Src="Http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="Text/javascript">$ (function () {$ ('. Input_test'). Bind ({focus:function () {if( This. Value = = This. DefaultValue) {  This. value=""; } }, Blur:function () {if( This. Value = =""){  This. Value = This. DefaultValue;} } }); }) </script>

As long as you add "input_test" to the input field of the implementation, you can easily implement the

The

Input input box hides/displays text when it gets/loses focus (jquery version)

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.