Disable candidates already selected in an election

From UnionCloud Support
Jump to: navigation, search

Currently every candidate in an election is shown on every positions drop down menu, which makes it easy for a student to accidentally select a candidate they have already chosen for another position, especially if you have lots of candidates.

If you have access to Global Javascript, you can paste in the code snippet below, which disables and greys out candidates that have already been selected for other positions, preventing students accidentally selecting a candidate twice (which would give an error) and making it easier to see who they've already selected.

var electionChosenCandidates = {};

$(function() {
    if (window.location.pathname.substring(0, 40) == '/evoting_election_votings/select_choices') {
        $('.uc-candidates-preferences select').each(function(index) {
            UpdateCandidates(this);
        });
        $('.uc-candidates-preferences select').bind('change', CandidateChange);
    }
});

function CandidateChange(type, data, handler) {
    UpdateCandidates(this);
}

function UpdateCandidates(selectElement) {
    var chosenOption = $(selectElement).val();
    var existingOption = ($(selectElement).attr('id') in electionChosenCandidates) ? electionChosenCandidates[$(selectElement).attr('id')] : -1;
    electionChosenCandidates[$(selectElement).attr('id')] = chosenOption;
    $('.uc-candidates-preferences select').not(selectElement).each(function(index) {
        $(this).find('option[value=' + chosenOption + ']').attr('disabled', true);
        if (existingOption >= 0) {
            $(this).find('option[value=' + existingOption + ']').attr('disabled', false);
        }
    });
}