Skip to content

Instantly share code, notes, and snippets.

@thotbox
Last active August 29, 2015 14:15
Show Gist options
  • Save thotbox/7a827a089fa20218ad74 to your computer and use it in GitHub Desktop.
Save thotbox/7a827a089fa20218ad74 to your computer and use it in GitHub Desktop.
JavaScript: Placeholder Label Fallback
// JS
if (!Modernizr.input.placeholder) {
$('.placeholder-fallback').addClass('fallback');
$('.has-placeholder').keyup(function() {
var fieldValue = $(this).val();
var fieldname = $(this).attr('name');
if (fieldValue === '') {
$('label[for=' + fieldname + ']').show();
} else {
$('label[for=' + fieldname + ']').hide();
}
});
$('.has-placeholder').change(function() {
var fieldValue = $(this).val();
var fieldname = $(this).attr('name');
if (fieldValue === '') {
$('label[for=' + fieldname + ']').show();
} else {
$('label[for=' + fieldname + ']').hide();
}
});
}
// HTML
<label class="placeholder-fallback" for="first_name">First Name</label>
<input class="has-placeholder" id="first_name" name="first_name" type="text" placeholder="First Name" required>
// CSS
.placeholder-fallback {
top: 15px;
left: 25px;
position: absolute;
display: none;
}
.placeholder-fallback.fallback {
display: block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment