javascript - Create a function to hide divs -


i want display tables when selection made in form , 'generate factsheet' button clicked. i've got working code individually hide other divs when displaying 1 interested in. since have several options in form (and hence several corresponding divs in respective tables enclosed), final code appears bulky. want write function hide other divs whiles displaying 1 interested in. code have:

    var tabledivs = ['tableopvdiv','tablepneumodiv','tablerotadiv'];         var selectedvaccine;         var selectedtablediv;      function generatefactsheetfunction(){         // selecting vaccine chosen dropdown         var e = document.getelementbyid("selectvaccine");         selectedvaccine = e.options[e.selectedindex].text;          console.log("selectedvaccine: ", selectedvaccine);           if (selectedvaccine=="rotavirus vaccine"){             selectedtablediv='tablerotadiv';             console.log("rotavirus selected");             hideothertables();           } else if (selectedvaccine=="polio vaccine"){             console.log("polio selected");             selectedtablediv='tableopvdiv';             hideothertables();          } else if (selectedvaccine=="pneumococcal vaccine"){             console.log("pneumo selected");             selectedtablediv='tablepneumodiv';             hideothertables();         }      }  function hideothertables(){ var testa = tabledivs.indexof(selectedtablediv); console.log("tabledivs[testa]: ", tabledivs[testa]); console.log("testa: ", testa); testb = tabledivs[testa]; console.log("testb: ", testb); document.getelementbyid(tabledivs[testa]).style.display="block"; /*var newtabledivs=tabledivs.splice(testa);*/         /*for (y=0;y<newtabledivs.length;y++){             document.getelementbyid(newtabledivs[y]).style.display="none";         }*/ } 

the uncommented part works fine. in commented part, want array elements other selectedvaccine, want display be:

document.getelementbyid(tabledivs[testa]).style.display="none"; 

i cannot splice data because selections repititive (the selections form). way set visibility of tabledivs associated other selections none.

why should change display property of each , every division seperately? give common class name divisions , hide them @ once , display required table.

$(".yourclass").hide(); document.getelementbyid(tabledivs[testa]).style.display="block"; 

you have use jquery library too.

if not familiar jquery use loop hide tables first , display required table.

for (y=0;y<tabledivs.length;y++){//you need not create newtabledivs   document.getelementbyid(tabledivs[y]).style.display="none"; } document.getelementbyid(tabledivs[testa]).style.display="block"; 

i.e have interchange order of execution. ;)


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -