I managed to realize a footnotes “system”, with a pop-up at the bottom of a window (as I said in another, recent, post). My present code works, but to close the bottom pop-up I have either to scroll the page or to click on a “x” inside each pop-up.
I wonder if, and how would be possible to close the pop-up by clicking anywhere (and not only, or not at all, over the “x” inside the pop-up)?
I tried adding window.addEventListener("click", handler("close")); after (or even replacing) window.addEventListener("scroll", handler("close"));
But unsuccessfully.
My whole js code is:
function getFootnoteContent(index) {
const id = "fn:" + index;
const fn = document.getElementById(id);
return fn.innerHTML.trim();
};
function footnotePopup(showIndex, showCloseBtn) {
const popupWrapper = document.querySelector("#popup-wrapper");
// Set whether to display index and/or close button. Default is true for both
if (showIndex === undefined) {
showIndex = true;
}
if (showCloseBtn === undefined) {
showCloseBtn = false;
}
// Create main container that will hold footnote content
const popupContent = popupWrapper.appendChild(document.createElement("div"));
popupContent.id = "popup-content";
let popupIndex = null;
if (showIndex) {
popupIndex = popupWrapper.insertBefore(document.createElement("div"), popupContent);
popupIndex.id = "popup-index";
}
let popupCloseButton = null;
if (showCloseBtn) {
popupCloseButton = popupWrapper.appendChild(document.createElement("div"));
popupCloseButton.innerHTML = "[x]";
popupCloseButton.id = "popup-close";
}
// Remove redundant [return] links from footnote list (optional)
const fnReturns = document.querySelectorAll("a.footnote-return");
fnReturns.forEach(function(fnReturn) {
const parent = fnReturn.parentNode;
parent.removeChild(fnReturn);
});
const fnRefs = document.querySelectorAll("a[id^='fnref:']");
fnRefs.forEach(function(fnRef) {
fnRef.addEventListener("click", handler("refs", fnRef));
});
window.addEventListener("scroll", handler("close"));
//window.addEventListener("click", handler("close"));
if (showCloseBtn) {
popupCloseButton.addEventListener("click", handler("close"));
}
function handler(type, node) {
return function(event) {
if (type === "close") {
popupWrapper.style.display = "none";
}
if (type === "refs") {
event.preventDefault();
const index = node.id.substring(6);
if (showIndex) {
popupIndex.innerHTML = index + ".";
}
popupContent.innerHTML = getFootnoteContent(index);
popupWrapper.style.display = "flex";
}
};
};
};
footnotePopup(false, true);
I believe detecting a click event on the body or HTML of the page would give you the hook you need to then run any action. Keep in mind that events bubble up to higher level elements.
If the user clicked on a refs link, not only should you prevent the default, you should prevent propagation.
Then you can bind a close handler to a higher level object, like the body.