I would like to know how can I define a bigger variable for a set of variables that I have in javascript: showFootnotesPanel();, showReferencesPanel();, showImagesPanel();, showInformationPanel();.
Would it be something like this?
function showPanel() { var x = [showFootnotesPanel();showReferencesPanel();showImagesPanel();showInformationPanel();] } Update:
I have this function that used to open a side panel on the right side and color the content:
var els = document.getElementsByClassName('change-color'), target = document.getElementsByClassName('resources'), changeColor = function(a) { elements = document.getElementsByClassName("note"); for (var i = 0; i < elements.length; i++) { console.log(elements[i]) elements[i].style.backgroundColor = ""; } target = a.getAttribute('href'); element = document.querySelector('[data-id="' + target.substring(1, target.length) + '"]'); element.style.backgroundColor = a.getAttribute('data-color'); }; for (var i = els.length - 1; i >= 0; --i) { els[i].onclick = function() { showFootnotesPanel(); changeColor(this); } Now I have 4 side panels that need to respond to the same script, and I thought that by defining something like showPanel() is showFootnotesPanel() or showReferencesPanel() or showImagesPanel() or showInformationPanel() I might simplify things, so the last line of the script would be this instead just:
els[i].onclick = function(){showPanel();changeColor(this);} Update 2:
Or is it possible to do this with the logical operator OR?
els[i].onclick = function(){showFootnotesPanel(); || showReferencesPanel(); || showImagesPanel(); || showInformationPanel();changeColor(this);} Update 3:
This is the new script that I am using to hide and show the panels:
function showPanel(myPanel) { var elem = document.getElementById(myPanel); if (elem.classList) { console.log("classList supported"); elem.classList.toggle("show"); } else { var classes = elem.className; if (classes.indexOf("show") >= 0) { elem.className = classes.replace("show", ""); } else { elem.className = classes + " show"; } console.log(elem.className); } } function hideOthers(one, two, three, four) { if (one > "") { var elem1 = document.getElementById(one); var classes = elem1.className; elem1.className = classes.replace("show", ""); } if (two > "") { var elem2 = document.getElementById(two); var classes = elem2.className; elem2.className = classes.replace("show", ""); } if (three > "") { var elem3 = document.getElementById(three); var classes = elem3.className; elem3.className = classes.replace("show", ""); } if (four > "") { var elem4 = document.getElementById(four); var classes = elem4.className; elem4.className = classes.replace("show", ""); } return; } And this is the script that calls the panels and highlights the text on them:
var els = document.getElementsByClassName('change-color'), target = document.getElementsByClassName('resources'), changeColor = function(a) { elements = document.getElementsByClassName("note"); for (var i = 0; i < elements.length; i++) { console.log(elements[i]) elements[i].style.backgroundColor = ""; } target = a.getAttribute('href'); element = document.querySelector('[data-id="' + target.substring(1, target.length) + '"]'); element.style.backgroundColor = a.getAttribute('data-color'); }; for (var i = els.length - 1; i >= 0; --i) { els[i].onclick = function() { hideOthers('footnotes-section', 'references-section', 'images-section', 'information-section'); showPanel('references-section'); changeColor(this); } } Thank you!