handleSelectElement function

Last updated: 2026-03-04T23:21:38.398Z

Metrics

LOC: 30 Complexity: 4 Params: 2

Signature

handleSelectElement(element: HTMLSelectElement, value: string): : void

Source Code

function handleSelectElement(element: HTMLSelectElement, value: string): void {
  const options = Array.from(element.options);

  // Try to match by value first
  const byValue = options.find((opt) => opt.value === value);
  if (byValue) {
    element.value = byValue.value;
    element.dispatchEvent(new Event("change", { bubbles: true }));
    return;
  }

  // Try to match by text
  const byText = options.find((opt) =>
    opt.text.toLowerCase().includes(value.toLowerCase()),
  );
  if (byText) {
    element.value = byText.value;
    element.dispatchEvent(new Event("change", { bubbles: true }));
    return;
  }

  // Pick a random non-empty option
  const validOptions = options.filter((opt) => opt.value);
  if (validOptions.length > 0) {
    const random =
      validOptions[Math.floor(Math.random() * validOptions.length)];
    element.value = random.value;
    element.dispatchEvent(new Event("change", { bubbles: true }));
  }
}

No outgoing dependencies.

Impact (Incoming)

graph LR handleSelectElement["handleSelectElement"] applyValueToField["applyValueToField"] applyValueToField -->|calls| handleSelectElement style handleSelectElement fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handleSelectElement "0a5f5d5c152645bc.html" click applyValueToField "59a962012828c5cb.html"
SourceType
applyValueToField calls