Created
April 15, 2011 04:35
-
-
Save kentbrew/921146 to your computer and use it in GitHub Desktop.
Placeholders for Everyone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (w, d, a) { | |
var $ = w[a.k] = {}; | |
$.a = a; | |
$.w = w; | |
$.d = d; | |
$.f = (function () { | |
return { | |
focus: function (e) { | |
var v = e || $.w.event, el = $.f.getEl(v), span = $.f.getPrev(el); | |
if (span) { | |
span.style.display = 'none'; | |
} | |
}, | |
blur: function (e) { | |
var v = e || $.w.event, el = $.f.getEl(v), span = $.f.getPrev(el); | |
if (span && span.tagName === 'SPAN') { | |
if (!el.value) { | |
// field has no value; show label | |
span.style.display = 'block'; | |
} else { | |
// field has value; hide label | |
span.style.display = 'none'; | |
} | |
} | |
}, | |
down: function (e) { | |
var v = e || $.w.event, el = $.f.getEl(v); | |
el.style.display = 'none'; | |
$.f.getNext(el).focus(); | |
}, | |
getEl: function (v) { | |
var el; | |
if (v.target) { | |
el = (v.target.nodeType === 3) ? v.target.parentNode : v.target; | |
} | |
else { | |
el = v.srcElement; | |
} | |
return el; | |
}, | |
getPrev : function (el) { | |
var prevSib = el.previousSibling; | |
if (prevSib && prevSib.nodeType !== 1) { | |
prevSib = prevSib.previousSibling; | |
} | |
return prevSib; | |
}, | |
getNext : function (el) { | |
var nextSib = el.nextSibling; | |
if (nextSib && nextSib.nodeType !== 1) { | |
nextSib = nextSib.nextSibling; | |
} | |
return nextSib; | |
}, | |
listen : function (el, ev, fn) { | |
if(typeof $.w.addEventListener !== 'undefined') { | |
el.addEventListener(ev, fn, false); | |
} else if(typeof $.w.attachEvent !== 'undefined') { | |
el.attachEvent('on' + ev, fn); | |
} | |
}, | |
makePlaceholder : function (input, placeholder) { | |
var span, rule; | |
if (input.type === 'text' || input.type === 'password' ) { | |
span = $.d.createElement('SPAN'); | |
span.innerHTML = placeholder; | |
span.style.position = 'absolute'; | |
span.style.lineHeight = input.offsetHeight + 'px'; | |
for (rule in $.a.style) { | |
if ($.a.style.hasOwnProperty(rule)) { | |
span.style[rule] = $.a.style[rule]; | |
} | |
} | |
if (input.value) { | |
span.style.display = 'none'; | |
} | |
input.parentNode.insertBefore(span, input); | |
$.f.listen(span, 'mousedown', $.f.down); | |
$.f.listen(input, 'focus', $.f.focus); | |
$.f.listen(input, 'blur', $.f.blur); | |
} | |
}, | |
init : function () { | |
// no need to do any of this for browsers that understand placeholders | |
if ($.d.createElement('INPUT').getAttribute('placeholder')) { | |
return; | |
} | |
var el, input, i, n, placeholder; | |
input = $.d.getElementsByTagName('INPUT'); | |
for (i = 0, n = input.length; i < n; i = i + 1) { | |
el = input[i]; | |
placeholder = el.getAttribute('placeholder'); | |
if (placeholder) { | |
$.f.makePlaceholder(el, placeholder); | |
} | |
} | |
} | |
}; | |
}()); | |
$.f.listen($.w, 'load', $.f.init); | |
}(window, document, {'k': 'INPUT-PLACEHOLDERS', 'style': {'fontSize':'87%', 'marginLeft':'5px', 'color':'#aaa'}})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p><input type="text" placeholder="username" value="foo" /></p> | |
<p><input type="password" placeholder="password1" /></p> | |
<p><input type="password" placeholder="password2" /></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment