Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Forked from rbk/index.php
Last active February 28, 2020 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/90527dca837e290d4405ba35432b235d to your computer and use it in GitHub Desktop.
Save nfsarmento/90527dca837e290d4405ba35432b235d to your computer and use it in GitHub Desktop.
Turn input placeholders into floating labels for WP Contact Form 7 fields
/*
*
* Description: Adds floating lables to all text inputs
*
*/
function ns_append_style_and_js( ){ ?>
<script type="text/javascript">
jQuery(document).ready(function($){
$(document).on('focus', 'input[type=text], input[type=email], textarea', function(e){
var ph = $(this).attr('placeholder');
if( ph ){
$(this).attr('ph', $(this).attr('placeholder') );
$(this).attr('placeholder', '');
$(this).animate({'padding-top':'20px', 'position' : 'relative'}, 100);
$(this).parent().append('<div class="floating-label">' + ph + '</div>');
$(this).parent().find('.floating-label').fadeIn();
}
});
});
</script>
<style type="text/css">
.wpcf7-form-control-wrap {
display: block;
}
.wpcf7-form-control-wrap .floating-label {
display: none;
position: absolute;
top: 3px;
z-index: 9;
left: 12px;
font-size: 12px;
}
</style>
<?php
}
add_action('wp_footer', 'ns_append_style_and_js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment