Как объединить два фрагмента кода в один?
Как правильно объединить одинаковые два фрагмента кода в один?
Отличием является лишь .blur и .each в начале кода.
$('input').each(function(){
$(this).parents('.form-group').addClass('focused');
});
$('input').blur(function(){
var inputValue = $(this).val();
if ( inputValue == "" ) {
$(this).removeClass('filled');
$(this).parents('.form-group').removeClass('focused');
} else {
$(this).addClass('filled');
}
});
$('input').focus(function(){
$(this).parents('.form-group').addClass('focused');
});
$('input').blur(function(){
var inputValue = $(this).val();
if ( inputValue == "" ) {
$(this).removeClass('filled');
$(this).parents('.form-group').removeClass('focused');
} else {
$(this).addClass('filled');
}
});
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
$('input').blur(function() {
var inputValue = $(this).val();
if (inputValue == "") {
$(this).removeClass('filled');
$(this).parents('.form-group').removeClass('focused');
} else {
$(this).addClass('filled');
}
}).blur();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>