Javascript List, Select default Items

I am trying to modify some code to better work for my needs.

It’s a list prompt that takes a text list from Keyboard Maestro and presents it to the user. The user then clicks the options they want.

Sometimes I need certain items in the list to be ‘selected’ when the list is presented. If the text items in the list have a suffix with |0|1 at the end then the item SHOULD NOT be selected. If the text items in the list have a suffix with |1|0 then the item SHOULD be selected.

Ex.

Test One|1|0

Test Two|0|1

Only “Test One” and “Test Two” should be displayed, but the |1|0 or |0|1 will tell the script to pre-select the list item or not.

I’m just trying to figure out what specific Javascript For Loop I need to add to the function init().

Here is the just some of the code, it should be the relevant bits:

// Create a reference to KeyboardMaestro, this helps avoid unnecessary global scope pollution
var KeyboardMaestro = window.KeyboardMaestro;

// Initialize an array to keep track of indices of selected items
var selectedIndices = [];

// Initialize an array to hold the selected items themselves
var selectedItems = [];

// A flag to determine if an item is currently being dragged
var isDragging = false;

// A flag to determine if an item is being marked or selected
var marking = false;

/**
 * Toggle the selection of a given item.
 * @param {HTMLElement} item - The list item to select or unselect.
 * @param {boolean} shouldSelect - Explicit instruction to select (true) or unselect (false). If undefined, toggle.
 */
function selectItem(item, shouldSelect) {
    // Convert the NodeList of 'li' elements to an array and get the index of the provided item
    var itemIndex = Array.from(document.querySelectorAll('li')).indexOf(item);

    // Check if the item's index is already in the selectedIndices array
    var index = selectedIndices.indexOf(itemIndex);

    if (index > -1) {
        // If the item is already selected
        if (shouldSelect !== true) {
            // If the function was called with explicit instruction not to select, remove the item from selections
            selectedIndices.splice(index, 1);
            item.firstChild.checked = false; // Uncheck the checkbox associated with the item
        }
    } else {
        // If the item is not currently selected
        if (shouldSelect !== false) {
            // If the function was called without explicit instruction to unselect, add the item to selections
            selectedIndices.push(itemIndex);
            item.firstChild.checked = true; // Check the checkbox associated with the item
        }
    }
    
    // Focus on the input element of type text, likely for further user input or interactions
    document.querySelector('input[type="text"]').focus();

    // Highlight the clicked item for visual feedback
    var selectedItem = document.querySelector('li.selected');
    if (selectedItem) {
        // If an item is already highlighted, remove the 'selected' class
        selectedItem.classList.remove('selected');
    }
    // Add the 'selected' class to the current item
    item.classList.add('selected');
}

/**
 * Initializes the application, sets up the list based on the KeyboardMaestro variables, and attaches event listeners.
 */
function init() {
    // Start with no initial selected item.
    window.selectedItemStart = null;

    // Process each line from the KeyboardMaestro prompt list variable.
    var promptList = KeyboardMaestro.GetVariable('Local__Prompt List').split('n');
    for (var i = 0; i < promptList.length; i++) {
        var parts = promptList[i].split('__'); // Split each line at '__'
        
        // Create new list item and checkbox elements.
        var li = document.createElement('li');
        var checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.disabled = true; // Make sure checkbox is not interactive.
        li.appendChild(checkbox);

        // Add a space after the checkbox.
        var space = document.createTextNode(' '); 
        li.appendChild(space);

        // If '__' is present in the line, use the part after for display and store the part before in a data attribute.
        // If not, use the whole line for both display and output.
        var text;
        if (parts[1]) {
            text = document.createTextNode(parts[1]);
            li.dataset.fullText = parts[0];
        } else {
            text = document.createTextNode(parts[0]);
            li.dataset.fullText = parts[0];
        }
        li.appendChild(text);

        // Add the created list item to the list.
        list.appendChild(li);
    }

    // Mark the first item in the list as selected.
    list.firstChild.classList.add('selected');
    
}

So far it looks like I need something like:

selectItem(li, true);

Any help is much appreciated.