www.webdeveloper.com
Results 1 to 4 of 4

Thread: problem using multiple variables in a Loop

  1. #1
    Join Date
    Nov 2007
    Posts
    18

    problem using multiple variables in a Loop

    Hi all ,

    I have the following script where it is generated from a embed java from a jsp page which writes the same script with different variables multi times the number of the length of a given list . My aim it to write a pure simplified JavaScript code to have the same logic of the following script. I even pasted my current code to do so but with no success.

    Here is the JavaScript generate from embed java from a jsp.
    Code:
    <script>
    var maxSize1 = 200;
    var ac1 = new AutoComplete(
                  document.getElementById('search_type'),		
                  document.getElementById('xqty_1'),		
                  document.getElementById('theDiv_1'),		
                  null,											
                  maxSize1 								
                  , function (text, refType) {
                      useLoadingMessage("Loading");
                      AutoCompleteBean.getMatchingCatalog(text, session_cookie, refType, reply1);
                  }
              );
    var reply1 = function onZoneInfo(acList) {
    	var numberOfEntries = acList.length;
    	if(numberOfEntries==0){
    		alert("No products found");
    	}
    	if(numberOfEntries>maxSize1){
    		alert("Too many products found");
    	}
    	if( (numberOfEntries==1) && (acList[0].substring(0,10) == "size_error") ){
    		alert("Too many products found");
    		acList.splice(0,1);
    	}
        ac1.repopulate(acList);
    }
    </script>
    
    
    <script>
    var maxSize2 = 200;
    var ac2 = new AutoComplete(
                  document.getElementById('search_type'),		
                  document.getElementById('xqty_2'),		
                  document.getElementById('theDiv_2'),		
                  null,											
                  maxSize2 								
                  , function (text, refType) {
                      useLoadingMessage("Loading");
                      AutoCompleteBean.getMatchingCatalog(text, session_cookie, refType, reply2);
                  }
              );
    var reply2 = function onZoneInfo(acList) {
    	var numberOfEntries = acList.length;
    	if(numberOfEntries==0){
    		alert("No products found");
    	}
    	if(numberOfEntries>maxSize1){
    		alert("Too many products found");
    	}
    	if( (numberOfEntries==1) && (acList[0].substring(0,10) == "size_error") ){
    		alert("Too many products found");
    		acList.splice(0,1);
    	}
        ac2.repopulate(acList);
    }
    </script>
    
    
    <script>
    var maxSize3 = 200;
    var ac3 = new AutoComplete(
                  document.getElementById('search_type'),		
                  document.getElementById('xqty_3'),		
                  document.getElementById('theDiv_3'),		
                  null,											
                  maxSize1 								
                  , function (text, refType) {
                      useLoadingMessage("Loading");
                      AutoCompleteBean.getMatchingCatalog(text, session_cookie, refType, reply3);
                  }
              );
    var reply3 = function onZoneInfo(acList) {
    	var numberOfEntries = acList.length;
    	if(numberOfEntries==0){
    		alert("No products found");
    	}
    	if(numberOfEntries>maxSize1){
    		alert("Too many products found");
    	}
    	if( (numberOfEntries==1) && (acList[0].substring(0,10) == "size_error") ){
    		alert("Too many products found");
    		acList.splice(0,1);
    	}
        ac3.repopulate(acList);
    }
    </script>
    Here is my current code but with no success
    Code:
    <script>
    var itemsLength = 3;// get length of list
    var maxSize = 200;
    var ac1,ac2,ac3;
    var ac     = new Array(ac1,ac2,ac3);
    var xqty_  = new Array("xqty_1","xqty_2","xqty_3");
    var theDiv_= new Array("theDiv_1","theDiv_2","theDiv_3");
    
    for (var i = 0; i< itemsLength; i++) {
    
    ac[i] = new AutoComplete(
                  document.getElementById('search_type'),		
                  document.getElementById(xqty_[i]),		
                  document.getElementById(theDiv_[i]),		
                  null,					
                  maxSize 				
                  , function (text, refType) {
                      useLoadingMessage(loadingTxt);
                      AutoCompleteBean.getMatchingCatalog(text, session_cookie, refType,
                      function onZoneInfo(acList) {
    			var numberOfEntries = acList.length;
    			if(numberOfEntries==0){
    			 alert(noProductsTxt);
    			}
    			if(numberOfEntries>maxSize){
    			 alert(tooManyProductTxt);
    			}
    			if( (numberOfEntries==1) && (acList[0].substring(0,10) == "size_error") ){
    			    alert(tooManyProductTxt);
    				acList.splice(0,1);
    			}
    			ac[i].repopulate(acList);
    			}
                      );
                  }
              );
    }
    
    </script>
    It seems it is giving me on "ac[i].repopulate(acList);" null.

    I'll be grateful for any Suggestion or help !!

  2. #2
    Join Date
    Jan 2005
    Posts
    360
    ac1, ac2 are not the same thing as ac[1], ac[2] etc.

    Not sure which way you want to change things, probably better to change the first block of code?

  3. #3
    Join Date
    Nov 2007
    Posts
    18
    Quote Originally Posted by omnicity View Post
    ac1, ac2 are not the same thing as ac[1], ac[2] etc.

    Not sure which way you want to change things, probably better to change the first block of code?
    Thanks omnicity for your reply,
    What actually I did, I create an array of variables ac1, ac2 ,... then in the loop I referred to them from ac[0] as ac1 and ac[1] as ac2 and so.

    So What do you suggest me to change??

  4. #4
    Join Date
    Jan 2005
    Posts
    360
    Sorry, I didn't quite follow what was going on there. Not the most beautiful coding style, but not a big problem either.

    What _is_ a problem is trying to call a function on an object _inside_ its declaration. If you look closely at the first script, you will see that the line equivalent to:
    Code:
    ac[i].repopulate(acList);
    happens _after_ the AutoComplete() function body has been closed, whereas in your second script the entire block is within the function body.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles