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.
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.
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.
Bookmarks