[thelist] Javascript var + string

Fabio Gomes flgomes at fazenda.sp.gov.br
Mon Jul 7 13:15:29 CDT 2003


Jason Handby wrote:
>>   In searching of a smaller code in my javascript I'm looking for some 
>>code to replace the following lines (for instance):
>>
>>   if(document.form.textfield_1.value == ""){
>>     return false;
>>   }
>>   if(document.form.textfield_2.value == ""){
>>     return false;
>>   }
>>   if(document.form.textfield_3.value == ""){
>>     return false;
>>   }
>>
>>   With those:
>>
>>   for(COUNT=0; COUNT<=3;COUNT++){
>>     var TheTextField = 'document.form.textfield_' + COUNT + '.value';
>>     if(TheTextField == ""){
>>       return false;
>>     }
>>   }
>>
>>   Since the 3 field above have almost the same name I tried to write 
>>the  above code.
>>   Yes, I know this is not going to work. But, what should be the 
>>working code based on the code above? Can I have any examples?
> 
> 
> This should work:
> 
> 	for (COUNT=0; COUNT<3; COUNT++)
> 	{
> 		if (document.form.elements['textfield_' + COUNT].value == "")
> 		{
> 			return false;
> 		}
> 	}
> 
> 
> 
> Jason
> 
   Oh my... The easiest things are the worst to be guessed. Thank you 
very much. I'm going to test it in my code.

   What a shame... A similar code in perl is the simpliest thing to do, 
but I was giving up in doing this with javascript.

Thank you again,
Fábio Gomes
-- 
@_{2**2..6*6}=split//,"aiGsDDbooe  eoin m-IsvveoF Tenlmt";print 
values%_,"\n"



More information about the thelist mailing list