I have form with an "add field" button that does an Ajax call to add a new field.
Each field contains a list of options to choose from. When is public
is Yes, I need to remove option value = "contacts" and "file upload" from the drop and when the "Add field" button is clicked. If the is public is no, I need to show the option value "contacts" and "file upload".
My code works, but I feel I can improve it.
publicFormValidation = function(){
$('#public').change(function(){
if(checkForm()){
$('.field_type option[value="file_upload"]').hide();
$('.field_type option[value="contacts"]').hide();
$('.add_many_fields').on('click',function(){
setTimeout(function(){
$('.field_type option[value="file_upload"]').hide();
$('.field_type option[value="contacts"]').hide();
}, 500);
});
}else {
var $contacts = $('.field_type option[value="contacts"]');
var $file_uploads = $('.field_type option[value="file_upload"]');
$contacts.show();
$file_uploads.show();
$('.add_many_fields').on('click',function(){
setTimeout(function(){
var $contacts = $('.field_type option[value="contacts"]');
var $file_uploads = $('.field_type option[value="file_upload"]');
$contacts.show();
$file_uploads.show();
}, 500);
});
};
});
}
checkForm = function(){
var public_val = $('#public').val();
return (public_val == "true");
}
The reason I have a setTimeout
function is because when I needed to create a delay from when click to hide onto the DOM.