homepage Welcome to WebmasterWorld Guest from 54.211.68.132
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript while loop not getting vars
Jeru



 
Msg#: 4440513 posted 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++;
}

 

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4440513 posted 3:32 pm on Apr 13, 2012 (gmt 0)

while(i < '36'){

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

while(i < 36){

Jeru



 
Msg#: 4440513 posted 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?

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4440513 posted 8:07 pm on Apr 13, 2012 (gmt 0)

Where is document.aSearch.cF defined? I assume aSearch is a form element, but what is cF?

Jeru



 
Msg#: 4440513 posted 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.

Jeru



 
Msg#: 4440513 posted 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!

Fotiman

WebmasterWorld Senior Member fotiman us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4440513 posted 9:09 pm on Apr 14, 2012 (gmt 0)

Glad you got it sorted. :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved