Skip to content

Instantly share code, notes, and snippets.

@khripunovpp
Last active March 13, 2019 09:14
Show Gist options
  • Save khripunovpp/470a959acf2d2769712f9f6c99f541de to your computer and use it in GitHub Desktop.
Save khripunovpp/470a959acf2d2769712f9f6c99f541de to your computer and use it in GitHub Desktop.
isFocus
var isFocus = function() {
var fieldEl = '.form__field',
groupEl = '.form__group',
labelEl = '.form__label',
onfocusClass = 'onfocus',
value;
$('body').addClass('js-placeholder')
$(fieldEl).each(function() {
value = $(this).val();
$(this).removeAttr('placeholder')
if (value.length > 0) $(this).closest(groupEl).addClass(onfocusClass).find(labelEl).fadeOut(200);
})
$(labelEl).on('click', function() {
$(this).closest(groupEl).find(fieldEl).focus()
});
$(fieldEl).on('focus', function() {
$(this).closest(groupEl).addClass(onfocusClass).find(labelEl).fadeOut(200);
});
$(fieldEl).on('blur', function() {
value = $(this).val();
if (value.length == 0) $(this).closest(groupEl).find(labelEl).fadeIn(200),
$(this).closest(groupEl).removeClass(onfocusClass);
if (value.length > 0) $(this).closest(groupEl).addClass(onfocusClass);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment