[thelist] Javascript var + string

Jason Handby jasonh at corestar.co.uk
Mon Jul 7 13:11:58 CDT 2003


>    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



More information about the thelist mailing list