I'm encountering an issue with the way these two functions interact when used in onclick calls of elements. Due to external circumstances beyond my control, I need to keep track of when and how elements are hidden.
Everything works perfectly as intended, except for one thing - the undefined check in the show function. I set a global variable when hiding elements and I aim to utilize it again when showing them. However, the problem arises when I discover that what I initially thought was a global variable turns out not to be so in the show function.
function branchShow(targetID, triggerID){
var target = document.getElementById(targetID);
var trigger = document.getElementById(triggerID);
var parentID = trigger.parentElement.parentElement.parentElement.parentElement.id;
var globalMemory = "wasIHiddenBefore_" + parentID
if (typeof window[globalMemory] !== "undefined"){
if (window[globalMemory]) {
console.log(globalMemory + " is evaluated true");
window[globalMemory] = false;
} else {
console.log(globalMemory + " is evaluated false");
target.setAttribute("style","display: block;");
}
} else {
console.log(globalMemory + " is undefined");
target.setAttribute("style","display: block;");
}
};
function branchHide(targetID, triggerID){
if (typeof i !== "undefined" ) {var iMemory = i;}
if (typeof j !== "undefined" ) {var jMemory = j;}
if (typeof k !== "undefined" ) {var kMemory = k;}
var target = document.getElementById(targetID);
target.setAttribute("style","display: none;");
//don't want to flag on load
if (hasLoadFinished){
window["wasIHiddenBefore_" + targetID] == true;
console.log("wasIHiddenBefore_" + targetID + " created as true");
}
.
.
.
The output from the console during a test run can be seen below. The 4th and 6th lines are particularly crucial in highlighting the problem at hand.
wasIHiddenBefore_1e16f2513f7842d5be352ca01b5c1c3f is undefined
wasIHiddenBefore_f82bdc0c527541e68fc405e9ac70015b is undefined
wasIHiddenBefore_2d869e44f4c44454a8415eecbd64061e created as true
wasIHiddenBefore_f82bdc0c527541e68fc405e9ac70015b created as true
wasIHiddenBefore_1e16f2513f7842d5be352ca01b5c1c3f is undefined
wasIHiddenBefore_f82bdc0c527541e68fc405e9ac70015b is undefined
If anyone has insights into why this behavior is occurring and suggestions on how to ensure that these dynamic variables act globally across functions, your input would be greatly appreciated. Thank you.