Last active
August 29, 2015 14:15
-
-
Save thotbox/7a827a089fa20218ad74 to your computer and use it in GitHub Desktop.
JavaScript: Placeholder Label Fallback
This file contains hidden or 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
// 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