Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Created May 15, 2010 19:39
Show Gist options
  • Save jonahlyn/402367 to your computer and use it in GitHub Desktop.
Save jonahlyn/402367 to your computer and use it in GitHub Desktop.
Inline Form Labels. http://jsbin.com/gist/402367
$(function(){
$('form label').addClass('inline');
$('input:text, input:password').each(function(){
$(this).attr({'value':''})
});
$('input:text, input:password').focus(function(){
$(this).addClass('selected');
if ($(this).val() == "") {
$(this).siblings('label.inline').animate({'opacity': '0.4'}, 'fast');
}
});
$('input:text, input:password').blur(function(){
$(this).removeClass('selected');
if($(this).val() == ""){
$(this).siblings('label.inline').animate({'opacity':'1'},'fast');
}
});
$('input:text, input:password').keypress( function(){
if ($(this).val() != "") {
$(this).siblings('label.inline').animate({'opacity': '0'}, 'fast');
}
});
$('input:text, input:password').keyup( function(){
if ($(this).val() != "") {
$(this).siblings('label.inline').animate({'opacity': '0'}, 'fast');
}
});
});
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Inline Form Labels</title>
<style>
<style type="text/css">
body {
font: 14px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;
}
form p {
float: left;
margin: 0;
position: relative;
padding: 0 5px 0 0;
text-align: left;
}
form label {
color: #666;
font-weight: bold;
}
form label.inline {
position: absolute;
left: 5px;
top: 5px;
}
form input {
border: 1px solid #999;
font-size: inherit;
margin: 0;
padding: 5px 3px;
}
form input#submit {
padding: 4px 3px;
}
form input.selected {
outline: 2px solid #CC0000;
-moz-outline-radius: 4px;
-moz-border-radius: 2px;
}
</style>
</style>
</head>
<body>
<form action="">
<p>
<label for="username">User Name</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" value="submit" id="submit" />
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment