Disable candidates already selected in an election

From UnionCloud Support
Revision as of 15:51, 15 December 2016 by Orry Verducci (talk | contribs) (Page creation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

$(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);
        }
    });
}