(function ($) { $(function () { $('#id_categories, #id_wikimedia_projects').each(function () { const otherCheckbox = $(this).find('input[value=0]'); const otherInputSelector = '#'.concat(this.id, '_other'); const otherInput = $(otherInputSelector); const otherLabelSelector = 'label'.concat('[for="', this.id, '_other"]'); const otherLabel = $(otherLabelSelector); const toggle = function () { const checked = otherCheckbox.prop('checked'); otherInput.prop('disabled', !checked); otherInput.prop('required', checked); otherLabel.toggleClass('required', checked); otherLabel.css('opacity', checked ? 1 : 0.3); if (checked) { otherInput.focus(); } else { otherInput.val(''); } }; toggle(); otherCheckbox.on('change', toggle); }); }); })(django.jQuery);