I have various elements that I want to show or hide depending on which activity the user is engaging in. Previously, my setDisplays function had a long list of parameters which needed to be set every time the function was called: e.g.
setDisplays("none","inline","inline","none","none","none","block","none","none","none","none","table","block","block");
Apart from being difficult to read and set, it was also inflexible. So I changed the function to take an array of objects: e.g.
setDisplays([{bingoDisplay:"none"},{bingoScore:"none"},{bingoWords:"none"},{book_overlay:"none"},{choiceDiv:"none"},{errDiv:"none"},{imageDiv:"block"},{livesDispTop:"none"},{phonDisplay:"none"},{score:"none"},{timer:"none"},{phonUSelect:"none"},{numUSelect:"none"},{vocSelect:"table"}]);
The modified function is:
function setDisplays(display) {
var display_name;
var display_setting;
for (var i=0; i<display.length; i++){
display_name=Object.keys(display[i]);
display_setting=display[i][display_name];
document.getElementById(display_name).style.display=display_setting;
}
}
I've done some initial testing, and it seems to work, but I'm new to objects, so before I commit and move everything over to the new function, could anyone tell me if there are unforeseen problems with this strategy?
I've searched around and could not find an example of an argument like this.
PS: I realise that Object.keys is not supported by older browsers, but I'm using HTML5 audio extensively, so need to create a redirect page for them anyway.