javascript - Call script from another script -


in html5 have dropdown menu . when choosing different options hide or show different parts of page. here script:

document     .getelementbyid('target')     .addeventlistener('change', function () {         'use strict';         var vis = document.queryselector('.vis'),                target = document.getelementbyid(this.value);         if (vis !== null) {             vis.classname = 'inv';         }         if (target !== null ) {             target.classname = 'vis';         } }); 

however want now, in script preload option dropdown. can script:

setselectedindex(document.getelementbyid('target'),'content_1'); function setselectedindex(s, valsearch)     {     // loop through items in drop down list     (i = 0; i< s.options.length; i++)     {      if (s.options[i].value == valsearch)         {         // item found. set property , exit         s.options[i].selected = true;         break;         }     }     return; } 

this problem comes up, dropdow value want, part want shown when choosing option won't come up.

i recommend moving anonymous onchange function named function can call once onload, , again onchange.

here function wrote:

function setcontent(id) {   //get current visible content    var vis = document.queryselector('.vis');   //get target element id    var target = document.getelementbyid(id);   //make current vis element inv    if (vis) vis.classname = "inv";   //make target element vis    if (target) target.classname = 'vis'; } 

and fiddle

edited: got rid of queryselectorall stick closer op original code , updated fiddle. clarified , commented code.


Comments