const getTimezone = function () {
const date = `${new Date()}`;
const timezoneInitial = date
.slice(date.indexOf("(") + 1, date.indexOf(")"))
.split(" ")
.map((word) => word.at(0))
.join("");
const timezone = date.slice(date.indexOf("(") + 1, date.indexOf(")"));
console.log([timezone, timezoneInitial]);
return [timezone, timezoneInitial];
};
const timezoneContainer = document.querySelector(
"select[data-name = Timezone]"
);
const allOptions = `
`;
timezoneContainer.insertAdjacentHTML("afterbegin", allOptions);
const clickOnSelectedElement = function (element) {
element.selected = true;
};
const setField = function () {
let selectedElement;
const timezone = `${getTimezone()[0]}`;
const timezoneArr = Array.from(timezoneContainer.children);
const result = timezoneArr.find((opt) => opt.value === timezone);
switch (result) {
case undefined:
const timezoneNew = getTimezone()[0];
const timezoneInitial = getTimezone()[1];
console.log(timezone, timezoneInitial);
html = ``;
timezoneContainer.insertAdjacentHTML("afterbegin", html);
selectedElement = document.querySelector(
`#Timezone option[value="${timezoneNew}"]`
);
console.log(
"%c Case undefined",
"color: blue; font-size: 1rem; font-weight:600"
);
break;
default:
selectedElement = result;
console.log(
"%c Case default",
"color: green; font-size: 1rem; font-weight:600"
);
break;
}
clickOnSelectedElement(selectedElement);
timezoneContainer.addEventListener("change", function () {
const otherField = document.querySelector("#other");
if (!(this.value === "Other")) {
otherField.style.display = "none";
otherField.removeAttribute("required");
this.setAttribute("required", true);
return;
}
this.removeAttribute("required");
otherField.setAttribute("required", true);
otherField.style.display = "block";
});
};
setField();