2023-02-21 22:49:57 +00:00
|
|
|
/**
|
|
|
|
* @description Sync select field with input field until user focuses it.
|
|
|
|
* @param {HTMLSelectElement} sourceElementSelector
|
|
|
|
* @param {HTMLInputElement} targetElementSelector
|
|
|
|
*/
|
|
|
|
function syncSelectInputUntilChanged(
|
|
|
|
sourceElementSelector,
|
|
|
|
targetElementSelector
|
|
|
|
) {
|
|
|
|
const sourceElement = document.querySelector(sourceElementSelector);
|
|
|
|
const targetElement = document.querySelector(targetElementSelector);
|
|
|
|
function sourceElementHandler(event) {
|
|
|
|
let selected = event.target.value;
|
|
|
|
let selectedValue = document.querySelector(
|
|
|
|
`#id_game option[value='${selected}']`
|
|
|
|
).textContent;
|
|
|
|
targetElement.value = selectedValue;
|
|
|
|
}
|
|
|
|
function targetElementHandler(event) {
|
|
|
|
sourceElement.removeEventListener("change", sourceElementHandler);
|
|
|
|
}
|
2023-02-20 17:21:48 +00:00
|
|
|
|
2023-02-21 22:49:57 +00:00
|
|
|
sourceElement.addEventListener("change", sourceElementHandler);
|
|
|
|
targetElement.addEventListener("focus", targetElementHandler);
|
|
|
|
}
|
2023-02-20 17:21:48 +00:00
|
|
|
|
2023-02-21 22:49:57 +00:00
|
|
|
window.addEventListener("load", () => {
|
|
|
|
syncSelectInputUntilChanged("#id_game", "#id_name");
|
|
|
|
});
|