Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    93
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Arrow simple onload javascript problem

    I need to load an array when my page loads which im doint by calling the arrayEmploye() function

    Code:
    function arrayEmploye()
    {
    	var arrayEmp=[];
    	var selobj2=document.getElementById('employe');
    	for (var zxc0=0;zxc0<selobj2.options.length;zxc0++)
    	{
    		arrayEmp.push([zxc0,selobj2.options[zxc0].text]); // Note this line
    	}
    	return arrayEmp;
    	//alert(arrayEmploye);
    }
    After that, in another function, i need to have the array (arrayEmp) available.

    Code:
    function Select(obj,selid)
    {
    	alert(arrayEmp);
    	var txt=obj.value;
    	var selobj=document.getElementById(selid);
    	var ary=[];
    	
    	for (var zxc0=0;zxc0<selobj.options.length;zxc0++)
    	{
    		ary.push([zxc0,selobj.options[zxc0].text]); // Note this line
    	}
    	
    	
    	//ary=ary.sort(Sort);
    	selobj.selectedIndex=0;
    	for (var zxc1=0;zxc1<ary.length;zxc1++)
    	{
    		if (txt.toLowerCase()==ary[zxc1][1].substring(0,txt.length).toLowerCase())
    		{  // Note this line checks text in field 1
    			selobj.selectedIndex=ary[zxc1][0];  // Note this line index in field 0
    			break;
    		}
    	}
    }
    How can i do this?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    simply call the function onload:

    onload=arrayEmploye;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    93
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    simply call the function onload:

    onload=arrayEmploye;
    This is what i'm trying to avoid... because my arrayEmploye function is long to run, i would like to run it once, store it somewhere and access it whenever i want to later on... Do you understand.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Sound tricky... You should run it sometime, and when running will take the same long time...

    Well, you may run it as first line in your second function

    function Select(obj,selid)
    {
    var arrayEmp= arrayEmploye();
    ..the rest of the code
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Mar 2006
    Posts
    93
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    Sound tricky... You should run it sometime, and when running will take the same long time...

    Well, you may run it as first line in your second function

    function Select(obj,selid)
    {
    var arrayEmp= arrayEmploye();
    ..the rest of the code
    }
    is there a way to call the arrayEmploye() function in the Select function the first time and then store the value. Then, the second time i call the Select function, it doesn't call the arrayEmploye function?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Make the array a global variable (push it outside the function)

    var arrayEmp=[];
    function arrayEmploye()
    {
    var selobj2=document.getElementById('employe');
    for (var zxc0=0;zxc0<selobj2.options.length;zxc0++)
    {
    arrayEmp.push([zxc0,selobj2.options[zxc0].text]); // Note this line
    }

    }
    ....
    Last edited by Kor; 05-24-2006 at 05:28 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •