简体   繁体   中英

call javascript function within function

Javascript Nub.

Running a function that checks the state of 5 selectboxes, if their value is "Select" i want to run a function to populate them based on another value. I can do this within the first function but it gets very long.

abbreviated version.

function chek{

var tligc = document.getElementById('assigc').innerHTML;   
var tligd = document.getElementById('assigd').innerHTML;



 if(tligc == 'Select'){document.getElementById('otherAgency3').className = 'textBox';
    return setass(3);
       }      

if(tligd == 'Select'){document.getElementById('otherAgency4').className = 'textBox'
 return setass(4);
}

Etc,

function setass(value){  

    var KaRa = document.getElementById('assig').innerHTML;

                           if(KaRa =='Planning Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Situation Unit", "Situation Unit", true, false)
 document.getElementById('otherAgency'+[value]).options[2]=new Option("Management Support", "Management Support", true, false)  
         return  }
else if(KaRa =='Operations Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Staging Area Manager", "Staging Area Manager", true, false)
    return }
else if(KaRa =='Logistics Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Supply Unit", "Supply Unit", true, false)
 document.getElementById('otherAgency'+[value]).options[2]=new Option("Communications Support", "Communications Support", true, false)  
 document.getElementById('otherAgency'+[value]).options[3]=new Option("Facilities Unit", "Facilities Unit", true, false)
        return }
    else{document.getElementById('otherAgency'+[value]).options.length=0;
      return }
       }
 }    

Its populating the first option but not running the rest of the checking function for the remainer if tests, any pointers on how to run the function setass and then return to the main function again at the right point? Im assuming 'return' is not the correct command (if im WAY off just say and ill do it the long way). Thankyou

The remainder of the if statements will not be called because you are returning from the function if the first one is hit.:

if(tligc == 'Select') {
    document.getElementById('otherAgency3').className = 'textBox';
    return setass(3);
}  

The return function in javascript, will return immediately to the calling function, either with a value or without one. The code above is basically saying: "If you get to this point, run the setass function with a parameter of 3, and returns the result back to the caller of chek .

You need to remove this return statement if you want the method to continue without ending.

if(tligc == 'Select') {
    document.getElementById('otherAgency3').className = 'textBox';
    setass(3);
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM