Ver código fonte

librairie pour focus

bastien 13 anos atrás
pai
commit
ef044124a0
1 arquivos alterados com 56 adições e 0 exclusões
  1. 56 0
      web/js/formdefault/jquery.formdefaults.js

+ 56 - 0
web/js/formdefault/jquery.formdefaults.js Ver arquivo

@@ -0,0 +1,56 @@
1
+(function($) {
2
+  
3
+  $.fn.formDefaults = function(options) {
4
+    
5
+    var opts = $.extend({}, $.fn.formDefaults.defaults, options);
6
+    
7
+    return this.each(function() {
8
+      
9
+      var $this = $(this);
10
+      var $form = $this.parents("form");
11
+      
12
+      $this
13
+        .data("defaultValue", this.value)
14
+        .addClass("form-default-value-processed");
15
+      
16
+      if (opts.inactiveColor) {
17
+        $this.css("color", opts.inactiveColor);
18
+      }
19
+      
20
+      $this
21
+        .focus(function() {
22
+          if (this.value == $this.data("defaultValue")) {
23
+            this.value = '';
24
+            this.style.color = opts.activeColor;
25
+          }
26
+        })
27
+        .blur(function() {
28
+          if (this.value == '') {
29
+            this.style.color = opts.inactiveColor;
30
+            this.value = $this.data("defaultValue");
31
+          }
32
+        });
33
+      
34
+      if (!$form.data("defaultValueProcessed")) {
35
+        $form
36
+          .data("defaultValueProcessed", true)
37
+          .submit(function(e) {
38
+            $(this).find(".form-default-value-processed").each(function() {
39
+              var $el = $(this);
40
+              if ($el.data("defaultValue") == $el.val()) {
41
+                $el.val('');
42
+              }
43
+            });
44
+          });
45
+      }
46
+      
47
+    });
48
+    
49
+  };
50
+  
51
+  $.fn.formDefaults.defaults = {
52
+    activeColor: '#000', // Color of text when form field is active
53
+    inactiveColor: '' // Color of text when form field is inactive (ignored when not specified)
54
+  };
55
+
56
+}(jQuery));