Skip to content

Instantly share code, notes, and snippets.

@craveytrain
Created July 15, 2010 02:17
Show Gist options
  • Save craveytrain/476405 to your computer and use it in GitHub Desktop.
Save craveytrain/476405 to your computer and use it in GitHub Desktop.
Accessible Inline Form Labels
<form id="contact">
<ul>
<li>
<label for="name">Name</label>
<input type="text" id="name" name="name" value="" />
</li>
<li>
<label for="email">Email</label>
<input type="text" id="email" name="email" value="" />
</li>
<li>
<label for="verbiage">Message…</label>
<textarea id="verbiage" name="verbiage" rows="5" cols="25"></textarea>
</li>
</ul>
</form>
var oTxtFields = $('input,textarea');
$.each(oTxtFields, function(){
var label = $('label[for=' + $(this).attr('id') + ']');
label.addClass('overlayed');
if (!$(this).val() == '') {
label.hide();
}
$(this)
.focus(function(e){
$('label[for=' + $(e.target).attr('id') + ']').hide();
})
.blur(function(e){
if ($(e.target).val() == '') {
$('label[for=' + $(e.target).attr('id') + ']').show();
}
})
;
});
form li {
line-height: 1.5;
margin-bottom: 1.5em;
position: relative;
}
form label.overlayed {
position: absolute;
top: .15em;
left: .5em;
white-space: nowrap;
color: #999;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment