Fixed Util static method issue

Also fixed bug in Util.getElement
This commit is contained in:
unsettledgames
2021-07-10 11:05:56 +02:00
parent f81019830b
commit dbffc0b9da
6 changed files with 38 additions and 53 deletions

View File

@@ -1,35 +1,41 @@
// Acts as a public static class
class Util {
static getElement(elementOrElementId) {
return typeof elementOrElementId
? document.getElementById(elementOrElementId)
: elementOrElementId;
if (typeof(elementOrElementId) == "object") {
return elementOrElementId;
}
else if (typeof(elementOrElementId) == "string") {
return document.getElementById(elementOrElementId);
}
else {
console.log("Type not supported: " + typeof(elementOrElementId));
}
}
static getText(elementId) {
return this.getElement(elementId).textContent;
return Util.getElement(elementId).textContent;
}
static setText(elementId, text) {
this.getElement(elementId).textContent = text;
Util.getElement(elementId).textContent = text;
}
static getValue(elementId) {
return this.getElement(elementId).value;
return Util.getElement(elementId).value;
}
static setValue(elementId, value) {
this.getElement(elementId).value = value;
Util.getElement(elementId).value = value;
}
//add class .selected to specified element
static select(elementId) {
this.getElement(elementId).classList.add('selected');
Util.getElement(elementId).classList.add('selected');
}
//remove .selected class from specified element
static deselect(elementId) {
this.getElement(elementId).classList.remove('selected');
Util.getElement(elementId).classList.remove('selected');
}
//toggle the status of the .selected class on the specified element
static toggle(elementId) {
this.getElement(elementId).classList.toggle('selected');
Util.getElement(elementId).classList.toggle('selected');
}
}