Skip to content

Commit 89e7e68

Browse files
committed
fix: getting value of select elements not working at all
!nuf closes monkeytypegame#7244
1 parent 91d64dc commit 89e7e68

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

frontend/src/ts/utils/dom.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,19 @@ export class ElementWithUtils<T extends HTMLElement = HTMLElement> {
450450
return new ElementWithUtils(wrapperElement as T);
451451
}
452452

453+
private hasValue(): this is ElementWithUtils<ElementWithValue> {
454+
return (
455+
this.native instanceof HTMLInputElement ||
456+
this.native instanceof HTMLTextAreaElement ||
457+
this.native instanceof HTMLSelectElement
458+
);
459+
}
460+
453461
/**
454462
* Set value of input or textarea to a string.
455463
*/
456464
setValue(this: ElementWithUtils<ElementWithValue>, value: string): this {
457-
if (this.native instanceof HTMLInputElement) {
465+
if (this.hasValue()) {
458466
this.native.value = value;
459467
}
460468
return this as unknown as this;
@@ -465,10 +473,10 @@ export class ElementWithUtils<T extends HTMLElement = HTMLElement> {
465473
* @returns The value of the element, or undefined if the element is not an input or textarea.
466474
*/
467475
getValue(this: ElementWithUtils<ElementWithValue>): string | undefined {
468-
if (!(this.native instanceof HTMLInputElement)) {
469-
return undefined;
476+
if (this.hasValue()) {
477+
return this.native.value;
470478
}
471-
return this.native.value;
479+
return undefined;
472480
}
473481

474482
/**

0 commit comments

Comments
 (0)