A friendly text box that displays a hint in jquery plugin

Source: Internet
Author: User

The plugin implements the default display prompt in the text box, which disappears when the text box receives the focus. If no input or input is empty, the prompt reappears when the focus is lost.
At the same time it is very comfortable and simple to use, after the introduction of plugins and jquery, in the original text box with the style class (class= "Prompt-input") and set the value (value= "Your prompt") as the cue.

Like this:

1 class="prompt-input" type="text" value=' Your Prompt ' />

There is no change in how you get the value at the same time, so adding a plug-in effect to a completed project has no pressure on any changes.

Implementation JS:

1 /*2  *3 * version:1.2.04 * Author:jinglan.woo (a) gmail.com5 * date:2014.08.076  *7 * Friendly prompt text input box:8 * The text input box has focus prompt disappears9 * and prompt appears again when it 's out of focusTen  * One  */ A  -(function ($) { -$.fn.promptinput =function(prompt, fontcolor) { the         var$ This= $( This);//Current Incoming text box -prompt = prompt? Prompt: $ This. Val ();//The prompt that appears in the input box -FontColor = FontColor? FontColor: ' #ccc ';//color of the cue language -  +         var$promptInput = $ This. Clone ();//clones an incoming text box to show -  +$promptInput. addclass (' Prompt-input '). CSS (' Color ', FontColor) A. attr (' Prompt ', prompt). attr (' type ', ' text '). Removeattr (' name '). REMOVEATTR (' id ')) at. val (prompt);//Instantiate a text box for presentation -  - $promptInput -. Focusin (function() {//get rid of tips when getting focus -$( This). CSS (' Color ', '); -                 if($( This). val () = = $ ( This). attr (' Prompt ')) { in$( This). Val ('); -                 } to             }) +. Focusout (function() {//Show hints when losing focus -                 if($( This). Val (). replace (/\s/g, ') = = ") { the$( This). Val ($ ( This). attr (' Prompt ')). CSS (' Color ', fontcolor); *$( This). Next (). Val ('); $                 }Panax Notoginseng}). Change (function() {//when the value changes, assign a value to the current incoming text box at the same time -$( This). Next (). Val ($ ( This). Val ()); the             });  +  A$ This. attr (' type ', ' hidden '). Val (');//change the current incoming text box type to hidden field the$promptInput. InsertBefore ($ This);//Append clone to page at the same time +     }; - }) (jQuery); $  $$(function () { -$ ('. Prompt-input '). each (function(Index, Element) {//page load complete automatic detection. Prompt-input class, loading effect - $ (Element). Promptinput (); the     }); -});
Jquery.promptInput.js

Using HTML:

1 <!DOCTYPE HTML>2 <Head>3     <Scriptsrc= "Http://code.jquery.com/jquery-1.11.1.min.js"></Script>4     <Scriptsrc= "Jquery.promptInput.js"></Script>5     <Scripttype= "Text/javascript">6         $(function () {7             $('#signUp'). Click (function () {8 Alert ('Username:' + $('#username'). Val ());9             });Ten         }); One     </Script> A </Head> - <Body> -         <inputclass= "Prompt-input"type= "text"value= "Please enter user name"ID= "username" /> the         <DivID= "SignUp">Sign Up</Div> - </Body> - </HTML>

You can copy the above code directly, or you can download the full code and demo here.

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.