forked from henrypoydar/jquery-form-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.form_prompt.js
109 lines (90 loc) · 3.05 KB
/
jquery.form_prompt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* jQuery Form Input Prompt Plugin 0.5.1
*
* Seemingly populate form inputs with text that disappears when the field is focussed.
* Works by not actually modifying the form field at all, instead an overlay div with
* the prompt text is added to the DOM. This approach works better than direct
* form field modification with AJAX-submitted forms and components.
*
* This script will become unnecessary once target browsers support HTML 5 and the
* placeholder attribute for form fields.
*
* Usage
*
* $('input#first_name').form_prompt('Don');
* $('input#email').form_prompt(function() {
* return $(this).attr('alt');
* });
* $('textarea#description').form_prompt('Type your message here', {
* className: 'form-prompt-class'
* });
*
* Or, use without options and put a placeholder attribute on element
*
* $("input[type=text], textarea").form_prompt();
*
* Copyright (c) Henry Poydar ([email protected])
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Contributors: xaviershay, triplef, binary42
*
**/
(function($) {
// Setup jQuery object method based on the definition below
$.fn.form_prompt = form_prompt;
function form_prompt(text, options) {
var use_placeholder = false;
var prompt_text = '';
options = options || {};
// If no options are set, use tag's placeholder attribute
if(text) {
// If text is passed as a callback, evaluate it
if ($.isFunction(text)) {
prompt_text = text.call(this);
} else {
prompt_text = text;
}
} else {
use_placeholder = true;
}
// Evaluate options
var className = options.className || 'form-prompt-text';
var wrapperClassName = options.wrapperClassName || 'form-prompt-wrapper';
return this.each(function() {
var input = $(this);
var wrapper = $('<div style="position:relative;overflow:hidden;display:inline-block;" />');
if(use_placeholder)
prompt_text = input.attr('placeholder');
// Use empty placeholder attribute in Safari
if ($.browser.safari) {
input.attr('placeholder', '');
}
// This may need adjustment for MSIE ...
var priorClasses = wrapper.attr('class');
input.wrap(wrapper.addClass(wrapperClassName));
wrapper.attr('class', priorClasses);
if (input.val() == '') {
input.after("<div class='" + className + "'>" + prompt_text + "</div>");
} else {
input.after("<div class='" + className + "'></div>");
}
var wrapper = input.parent('.' + wrapperClassName);
var prompt = wrapper.find('.' + className);
prompt.css("position", "absolute");
prompt.css("top", "0");
prompt.css("left", "0");
prompt.css("z-index", "1000");
var selectInput = function() {
input.focus();
prompt.hide();
}
input.click(selectInput); // Form field is clicked
input.keyup(selectInput); // Form field is tabbed into
prompt.click(selectInput); // Prompt element is clicked
input.blur(function() {
if (input.val() == '') { prompt.show(); }
});
});
};
})(jQuery);