/artissime/html/js/jquery.watermarkinput.js

http://mbideasproject.googlecode.com/ · JavaScript · 81 lines · 48 code · 5 blank · 28 comment · 8 complexity · b739b12a85ab3145df7ce6b73046c245 MD5 · raw file

  1. /*
  2. * Copyright (c) 2007 Josh Bush (digitalbush.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use,
  8. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following
  11. * conditions:
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  17. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Version: Beta 1
  26. * Release: 2007-06-01
  27. */
  28. (function($) {
  29. var map=new Array();
  30. $.Watermark = {
  31. ShowAll:function(){
  32. for (var i=0;i<map.length;i++){
  33. if(map[i].obj.val()==""){
  34. map[i].obj.val(map[i].text);
  35. map[i].obj.css("color",map[i].WatermarkColor);
  36. }else{
  37. map[i].obj.css("color",map[i].DefaultColor);
  38. }
  39. }
  40. },
  41. HideAll:function(){
  42. for (var i=0;i<map.length;i++){
  43. if(map[i].obj.val()==map[i].text)
  44. map[i].obj.val("");
  45. }
  46. }
  47. }
  48. $.fn.Watermark = function(text,color) {
  49. if(!color)
  50. color="#aaa";
  51. return this.each(
  52. function(){
  53. var input=$(this);
  54. var defaultColor=input.css("color");
  55. map[map.length]={text:text,obj:input,DefaultColor:defaultColor,WatermarkColor:color};
  56. function clearMessage(){
  57. if(input.val()==text)
  58. input.val("");
  59. input.css("color",defaultColor);
  60. }
  61. function insertMessage(){
  62. if(input.val().length==0 || input.val()==text){
  63. input.val(text);
  64. input.css("color",color);
  65. }else
  66. input.css("color",defaultColor);
  67. }
  68. input.focus(clearMessage);
  69. input.blur(insertMessage);
  70. input.change(insertMessage);
  71. insertMessage();
  72. }
  73. );
  74. };
  75. })(jQuery);