This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/public/scripts/form.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-11-23 06:23:57 +00:00
import * as Data from "./data.js";
export async function populateSports(sportDropdown, selectedSportID = undefined) {
sportDropdown.innerHTML = "";
const sportsList = await Data.getSports();
let currentIndex = 0;
let selectedSportIndex;
sportsList.forEach(sport => {
const option = document.createElement('option');
option.text = sport.name;
option.value = sport.id;
sportDropdown.appendChild(option);
if(sport.id == selectedSportID) selectedSportIndex = currentIndex;
currentIndex++;
});
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
}
export async function addHiddenValue(name, value, form) {
const valueInput = document.createElement('input');
valueInput.setAttribute('name', name);
valueInput.setAttribute('value', value);
valueInput.setAttribute('type', 'hidden');
form.appendChild(valueInput);
}
export async function addRemoveFunction(removeButton, form, objectTitle) {
removeButton.addEventListener('click', async () => {
const verified = confirm(`This ${objectTitle} will be removed.`);
if(verified) {
await addHiddenValue('remove', 1, form);
form.submit();
}
});
}