6 lines
1.8 KiB
JavaScript
6 lines
1.8 KiB
JavaScript
/*
|
|
* Placeholder plugin for jQuery
|
|
* ---
|
|
* Copyright 2010, Daniel Stocks (http://webcloud.se)
|
|
* Released under the MIT, BSD, and GPL Licenses.
|
|
*/(function(e){function t(t){this.input=t;t.attr("type")=="password"&&this.handlePassword();e(t[0].form).submit(function(){t.hasClass("placeholder")&&t[0].value==t.attr("placeholder")&&(t[0].value="")})}t.prototype={show:function(e){if(this.input[0].value===""||e&&this.valueIsPlaceholder()){if(this.isPassword)try{this.input[0].setAttribute("type","text")}catch(t){this.input.before(this.fakePassword.show()).hide()}this.input.addClass("placeholder");this.input[0].value=this.input.attr("placeholder")}},hide:function(){if(this.valueIsPlaceholder()&&this.input.hasClass("placeholder")){this.input.removeClass("placeholder");this.input[0].value="";if(this.isPassword){try{this.input[0].setAttribute("type","password")}catch(e){}this.input.show();this.input[0].focus()}}},valueIsPlaceholder:function(){return this.input[0].value==this.input.attr("placeholder")},handlePassword:function(){var t=this.input;t.attr("realType","password");this.isPassword=!0;if(e.browser.msie&&t[0].outerHTML){var n=e(t[0].outerHTML.replace(/type=(['"])?password\1/gi,"type=$1text$1"));this.fakePassword=n.val(t.attr("placeholder")).addClass("placeholder").focus(function(){t.trigger("focus");e(this).hide()});e(t[0].form).submit(function(){n.remove();t.show()})}}};var n="placeholder"in document.createElement("input");e.fn.placeholder=function(){return n?this:this.each(function(){var n=e(this),r=new t(n);r.show(!0);n.focus(function(){r.hide()});n.blur(function(){r.show(!1)});if(e.browser.msie){e(window).load(function(){n.val()&&n.removeClass("placeholder");r.show(!0)});n.focus(function(){if(this.value==""){var e=this.createTextRange();e.collapse(!0);e.moveStart("character",0);e.select()}})}})}})(jQuery); |