Skip to content

Instantly share code, notes, and snippets.

@rodi01
Created January 31, 2012 09:27
Show Gist options
  • Save rodi01/1709614 to your computer and use it in GitHub Desktop.
Save rodi01/1709614 to your computer and use it in GitHub Desktop.
Nice input labels using jQuery and CSS
form li { position: relative; }
label {
position: absolute;
top: 5px; // adjust as needed
left: 5px;
}
//remove absolute position if the label comes after
//the input. Eg. a checkbox with a text next to it
input + label { position: static; }
<form action="/">
<ul>
<li>
<label for="username">Username</label>
<input id="username" type="text" autocomplete="off" />
</li>
<li>
<label for="password">Password</label>
<input id="password" type="password" autocomplete="off" />
</li>
<li>
<input type="submit" value="login" />
</li>
</ul>
</form>
//loop through all text inputs, password inputs and textarea
var inputs = $('input[type="text"], textarea, input[type="password"]');
// remove label on focus
inputs.focus(function(){
$('label[for="'+$(this).attr('id')+'"]').hide();
});
inputs.blur(function(){
var that = $(this);
if (that.val() == ""){
$('label[for="'+that.attr('id')+'"]').show();
}
});
inputs.blur();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment