My form has required fields that need to be completed. To alert users about blank fields, I have implemented the following code:
$(document).ready(function() {
$('input.required:text').focus(function() {
$(this).css({'background-color': '#FFF'});
});
$('input.required:text').blur(function() {
if ($(this).val() == '') {
$(this).css({'background-color': 'rgba(222, 41, 30, 1)'});
}
});
})
The issue arises when I use an "add" button to duplicate a hidden form using this function:
function addlocation() {
var clone = $('div.location-info-new').clone();
clone.addClass('location-info');
clone.find('form').addClass('ready');
clone.removeClass('location-info-new');
clone.appendTo('div.locations');
clone.find('input.required:text').focus(function() {
$(this).css({'background-color': '#FFF'});
});
clone.find('input.required').each(function (i, v) {
$(this).css({'background-color': 'rgba(222, 41, 30, 1)'});
});
}
To address this issue, I included background-color changes in the cloned form as they were not working initially. However, there is still a problem. The new form's fields correctly display in red initially and change to white upon focus. But, if a user focuses on an input box, leaves it empty, and removes focus, the input box does not revert back to red. This behavior is different from the original form where it works properly.
I am puzzled as to why the background-color changes from document.ready are not being applied to my cloned form. Can anyone shed light on this issue?