Skip to content

Instantly share code, notes, and snippets.

@ajaswa
Created November 3, 2011 16:16
Show Gist options
  • Select an option

  • Save ajaswa/1336927 to your computer and use it in GitHub Desktop.

Select an option

Save ajaswa/1336927 to your computer and use it in GitHub Desktop.
input label
<!doctype>
<html>
<head>
<link href="input-label.css" rel='stylesheet' type='text/css' />
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="input-label.js"></script>
</head>
<body>
<form>
<fieldset>
<span>
<label class='infield' for='field'>label</label>
<input id='field' />
</span>
</fieldset>
</form>
</body>
</html>
form {
margin:10px 0px;
}
form fieldset {
margin-bottom:20px;
}
form fieldset span {
margin:0 0 5px;
display:block;
position:relative;
}
form fieldset span label {
color:#ccc;
font-size:116%;
display:block;
position:absolute;
top:6px;
left:5px;
}
form fieldset span input {
background:#fff;
padding:4px;
font-size:116%;
color:#444;
}
$(document).ready(function (){
var inputs = $('form input');
inputs.each(function(){
if ( $(this).val() !== '') $(this).siblings('label').fadeOut()
}).on('focus', function(){
$(this).siblings('label').fadeOut()
}).on('blur', function(){
if ( $(this).val() === '') $(this).siblings('label').fadeIn()
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment