I'm having an issue with the interaction of these two functions. They are used in onclick calls of elements. Essentially (because of things outside my control) I need to remember when and what has hidden an element.
Everything is working exactly as I want, except for the undefined check in the show function. I create a global variable when I hide things and I want to use that again when I'm looking to show things. The only issue I have is that in the show function, it seems what I thought was a global variable is not.
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 console output from a sample execution is below. The 4th and the 6th line are the most critical in displaying the issue.
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
Does anyone know why I'm seeing what I'm seeing and what I can do to make these dynamic variables act globally from function to function? Thanks.
window["wasIHiddenBefore_" + targetID] == true;
is a comparison, not an assignment. Despite logging "… created as true" you never set the variable. You want window["wasIHiddenBefore_" + targetID] = true;
.