-
-
Save rodi01/1709614 to your computer and use it in GitHub Desktop.
Nice input labels using jQuery and CSS
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 { 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; } |
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 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> |
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
//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