Created
July 15, 2010 02:17
-
-
Save craveytrain/476405 to your computer and use it in GitHub Desktop.
Accessible Inline Form Labels
This file contains 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
<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> |
This file contains 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
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(); | |
} | |
}) | |
; | |
}); |
This file contains 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
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