/
index.html
170 lines (140 loc) · 6.6 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Alternative placeholder behavior - Robert Nyman</title>
<link rel="stylesheet" href="../css/base.css" type="text/css" media="screen">
<!--[if lte IE 8]>
<script src="../js/html5.js"></script>
<![endif]-->
<style>
label {
display: block;
font-weight: bold;
margin: 0 5px 5px;
}
input {
display: block;
margin: 0 5px 20px;
}
form input[data-placeholder] {
color: #aaa;
}
form input[data-placeholder].active {
color: #000;
}
</style>
</head>
<body>
<div id="container">
<header role="banner">
<h1>Alternative placeholder behavior <a href="../index.html">back to playground</a></h1>
<p>Mimicking the native placeholder behavior in iOS, i.e. not replacing placeholder text until the user actually starts typing.</p>
</header>
<div role="main">
<section id="main-content">
<h2>Placeholder demo</h2>
<form action="index.html" novalidate>
<label for="first-name">First name</label>
<input type="text" id="first-name" name="first-name" placeholder="E.g. Robert">
<label for="last-name">Last name</label>
<input type="text" id="last-name" name="last-name" placeholder="E.g. Nyman">
<label for="phone-no">Phone no</label>
<input type="tel" id="phone-no" name="phone-no" placeholder="What is your phone no?">
<label for="email-address">E-mail</label>
<input type="email" id="email-address" name="email-address" placeholder="Enter your e-mail address">
<label for="user-pwd">Password</label>
<input type="text" id="user-pwd" name="user-pwd" data-type="password" placeholder="Choose a password">
<label for="user-pwd-repeat">Repeat password</label>
<input type="text" id="user-pwd-repeat" name="user-pwd-repeat" data-type="password" placeholder="Repeat your entered password">
<input type="submit" value="Send to myself">
</form>
</section>
</div>
<footer id="page-footer" role="contentinfo">
Created by <a href="http://robertnyman.com/">Robert Nyman</a>
</footer>
</div>
<script>
var dataPlaceholders = document.querySelectorAll("input[placeholder]"),
l = dataPlaceholders.length,
// Set caret at the beginning of the input
setCaret = function (evt) {
if (this.value === this.getAttribute("data-placeholder")) {
this.setSelectionRange(0, 0);
evt.preventDefault();
evt.stopPropagation();
return false;
}
},
// Clear placeholder value at user input
clearPlaceholder = function (evt) {
if (!(evt.shiftKey && evt.keyCode === 16) && evt.keyCode !== 9) {
if (this.value === this.getAttribute("data-placeholder")) {
this.value = "";
this.className = "active";
if (this.getAttribute("data-type") === "password") {
this.type = "password";
}
}
}
},
restorePlaceHolder = function () {
if (this.value.length === 0) {
this.value = this.getAttribute("data-placeholder");
setCaret.apply(this, arguments);
this.className = "";
if (this.type === "password") {
this.type = "text";
}
}
},
clearPlaceholderAtSubmit = function (evt) {
for (var i=0, placeholder; i<l; i++) {
placeholder = dataPlaceholders[i];
if (placeholder.value === placeholder.getAttribute("data-placeholder")) {
placeholder.value = "";
}
}
};
for (var i=0, placeholder, placeholderVal; i<l; i++) {
placeholder = dataPlaceholders[i];
placeholderVal = placeholder.getAttribute("placeholder");
placeholder.setAttribute("data-placeholder", placeholderVal);
placeholder.removeAttribute("placeholder");
if (placeholder.value.length === 0) {
placeholder.value = placeholderVal;
if (placeholder.type === "password") {
placeholder.type = "text";
}
}
else {
placeholder.className = "active";
}
// Apply events for placeholder handling
placeholder.addEventListener("focus", setCaret, false);
placeholder.addEventListener("drop", setCaret, false);
placeholder.addEventListener("click", setCaret, false);
placeholder.addEventListener("keydown", clearPlaceholder, false);
placeholder.addEventListener("keyup", restorePlaceHolder, false);
placeholder.addEventListener("blur", restorePlaceHolder, false);
// Clear all default placeholder values from the form at submit
placeholder.form.addEventListener("submit", clearPlaceholderAtSubmit, false);
}
</script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-56164-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();
var sc_project=593937,
sc_partition=4,
sc_security="175a1fec";
</script>
<script src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div class="statcounter"><a class="statcounter" href="http://www.statcounter.com/"><img class="statcounter" src="http://c5.statcounter.com/counter.php?sc_project=593937&java=0&security=175a1fec" alt="free geoip"></a></div></noscript>
</body>
</html>