← All articles

2 min read

Disable select option if already selected in another select box

A jQuery script that disables select options already chosen in another select box, with a ProcessWire integration.

For a client project we needed a possibility to assign different properties to an object (the “object” is a room in this case).

But… if a property was selected before, then the possibility to add this property again should be disabled.

As we found no reliable solution after some googling, we wrote a JavaScript (jQuery) to the rescue.

Here you can see it in action

Screencast: properties of a room in the ProcessWire admin. Once a property is used, it can't be picked again.

Try it yourself

Pick a property in one row and it gets greyed out in all the others. Switch the check off to see what happens without it: duplicates creep in.

The playground runs this function on every change. It needs no jQuery: collect the chosen values, then disable each option that another select already uses.

function disableUsedOptions(container) {
    const selects = [...container.querySelectorAll("select")];
    const used = new Set(selects.map((select) => select.value).filter(Boolean));
    for (const select of selects) {
        for (const option of select.options) {
            // Keep the select's own choice enabled, or it couldn't show it anymore
            option.disabled = option.value !== "" && used.has(option.value) && option.value !== select.value;
        }
    }
}

const container = document.querySelector(".properties");
container.addEventListener("change", () => disableUsedOptions(container));
disableUsedOptions(container);

ProcessWire integration

We integrated a jQuery version of the script into ProcessWire where it should be used in conjunction with ProFields: Table and a page reference field (which is used for the property).

site/templates/scripts/admin.js

function disableUsedOptions($table) {
    $selects = $table.find("select");
    $selects.on("change", function () {
        $selects = $table.find("select");

        if (config.debug === true) console.log("In table:");
        if (config.debug === true) console.log($table);
        if (config.debug === true) console.log("there are " + $selects.length + " selects");
        if ($selects.length <= 1) return;
        let selected = [];

        $selects.each(function (index, select) {
            if (select.value !== "") {
                selected.push(select.value);
            }
        });

        if (config.debug === true) console.log("option values, that are being deactivated: " + selected);
        if (config.debug === true) console.log($(this));
        $table.find("option").prop("disabled", false);
        for (var index in selected) {
            $table
                .find('option[value="' + selected[index] + '"]:not(:selected)')
                .prop("disabled", true);
        }
    });
    $selects.trigger("change");
}

$(document).ready(function () {
    $tables = $("table.InputfieldTable:not(.InputfieldTableSearch)");
    $tables.each(function () {
        $table = $(this);
        disableUsedOptions($table);
    });
});

then add the following lines to site/templates/admin.php

$config->scripts->add($config->urls->templates . "scripts/admin.js");
$config->styles->add($config->urls->templates . "styles/admin.css");
// before the line that reads
require($config->paths->adminTemplates . 'controller.php');