I have created some custom input in which it show an animation when get focused. It's working fine when input fields are blank because when value of input is null/blank then input set to defaults (i.e State-1) (See jsFiddle) and when it has some value it remains on it's new state after the animation(State-2).
I want that all input must be checked on form load,it should check all the input fields, if those who have some value then the input must be on State-2 else on state-1
NOTE: If you have any better way to achieve this than mine then please share with me.
HTML Sample Code:
<div class="container">
<div class="form-wrapper">
<label>Name</label>
<input type="text" class="form-input">
</div>
</div>
JS :
$('.form-wrapper .form-input').focus(function () {
$(this).parent().find('label').addClass('hasValue');
$(this).addClass('hasValueInput');
});
$('.form-wrapper .form-input').blur(function () {
if (!$(this).val()) {
$(this).parent().find('label').removeClass('hasValue');
}
$(this).removeClass('hasValueInput');
});
CSS:
.form-wrapper label {
font-size: 110%;
position: absolute !important;
top: 10px;
box-sizing: border-box;
color: #80868b;
left: 20px;
z-index: 10;
width: auto;
padding: 1px;
transition: font-size 0.2s, transform 0.2s;
}
.form-input{
padding:5px;
}
.hasValue {
color: #000 !important ;
font-size: 90% !important;
font-weight: 700;
background-color: white;
z-index: 2;
transform: translate(0px, -10px);
}
.hasValueInput {
box-shadow: none !important;
border: 2px solid #17a2b8 !important ;
}