Welcome to WebmasterWorld Guest from 54.161.187.250

Forum Moderators: open

Message Too Old, No Replies

Javascript while loop not getting vars

   
3:01 pm on Apr 13, 2012 (gmt 0)



I am trying to do a javascript while loop to access certain form elements, but I cannot seem to get this right. it simply doesn't work. Can anyone look at it and point me in the right direction?

var i = 1;
while(i < '36'){

if(document.aSearch.cF[i].value != '') document.uSearch.Squery.value = "#"+document.aSearch.lF[i].value+"# "+document.aSearch.fF[i].value+" '"+document.aSearch.cF[i].value+"'";
//setFilter();
i++;
}
3:32 pm on Apr 13, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



while(i < '36'){

you're treating i as a string there. Try this:

while(i < 36){
5:26 pm on Apr 13, 2012 (gmt 0)



Thanks Fontiman, can't believe I missed that one...

I still have problems accessing the field though. When trying to access the fields like this:

document.uSearch.Squery.value = document. aSearch.cF|i|.value;


I am getting: document.aSearch.cF|i| is undefined.
Tried all kind of variations, but to no avail. I know the fields exist.

The | represent square brackets, I can't post with square brackets here.

Any ideas?
8:07 pm on Apr 13, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Where is document.aSearch.cF defined? I assume aSearch is a form element, but what is cF?
6:28 am on Apr 14, 2012 (gmt 0)



The fieldnames in the form (named aSearch) are named and id-ed cF1, cF2, cF3 etc.
6:36 am on Apr 14, 2012 (gmt 0)



Just realised my approach was all wrong, Javascript is pretty new to me.

I am now using:
document.getElementById('cF'+i).value;

And that works. Thanks Fotiman for your support!
9:09 pm on Apr 14, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Glad you got it sorted. :-)