I have a collection that contains a check box and an upload button, what I want is that by having a file loaded the check box is selected, any suggestions on how to do this?
Question
Question
Answer
This sample code assumes the class MyCollection for the collection, collectionupload for the file, and collectioncheckbox for the checkbox with a default value of "choice 1".
$('.collectionupload, .MyCollection > .cf-collection-append, button.btn.btn-danger.deleteFileBtn, .MyCollection').on("click change", checkboxuploads); function checkboxuploads() { $(".MyCollection .kx-repeatable > div > .rpx").each(function(ind) { var fileuploaded = $(this).find('.collectionupload > div > div > table > tbody > tr.file').length; var checkbox = $(this).find('.collectioncheckbox > div > div > fieldset > span > input[value=\"choice_1\"]'); if (fileuploaded) { checkbox.prop('checked', true); } else { checkbox.prop('checked', false); } }); }
Replies
Hi Edgar. In this sample code, my File Upload button is field 8, so replace q8 below with the name of your upload field. In addition, I am assuming that there is only one file that can be uploaded per File Upload Field. The checkbox being checked in my sample is field 9, with the default value of "choice 1".
When a file is uploaded, the box is checked. When it is deleted, the box is unchecked.
$(document).ready(function() { $('#q8').on("click change", function() { if ($('#q8 > div > div > table > tbody > tr.file').length) { $('input[value=\"choice_1\"]#Field9-0').prop('checked', true).change(); } else { $('input[value=\"choice_1\"]#Field9-0').prop('checked', false).change(); } $('button.btn.btn-danger.deleteFileBtn').on("click", function() { if ($('#q8 > div > div > table > tbody > tr.file').length) { $('input[value=\"choice_1\"]#Field9-0').prop('checked', true).change(); } else { $('input[value=\"choice_1\"]#Field9-0').prop('checked', false).change(); } }); }); });
Hi, Karina
I already implement the suggested code but it did not work for me, in the code do you use a table? is the same in the case of having it in a collection, what does my form is to fill a collection, with several documents that must be loaded, and in the same collection is a checkbox that indicates if the document is loaded.
The above code is for a standalone upload field and checkbox. You will have to modify it to iterate through each row of a collection.
This sample code assumes the class MyCollection for the collection, collectionupload for the file, and collectioncheckbox for the checkbox with a default value of "choice 1".
$('.collectionupload, .MyCollection > .cf-collection-append, button.btn.btn-danger.deleteFileBtn, .MyCollection').on("click change", checkboxuploads); function checkboxuploads() { $(".MyCollection .kx-repeatable > div > .rpx").each(function(ind) { var fileuploaded = $(this).find('.collectionupload > div > div > table > tbody > tr.file').length; var checkbox = $(this).find('.collectioncheckbox > div > div > fieldset > span > input[value=\"choice_1\"]'); if (fileuploaded) { checkbox.prop('checked', true); } else { checkbox.prop('checked', false); } }); }
Hi Karina,
Thanks for the code, I tryed it and works perfectly