Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting number to string

Status
Not open for further replies.

webmast

Programmer
Dec 21, 2001
57
IN
i have something like this. In a page i'll be displaying a set of questions retrieved from the backend all with answers
as radio button choices yes or no. The number of questions generated may vary.so i used the question ID as name for the respective radiobuttons.When i click on submit answers
i want to check whether any question has been left unanswered(no default checked value).if so the alert is shown.In the below function i have first retrieved the count of questions and in a loop trying to check the values of the radio buttons. Since the name of radio buttons are 1,2,3.... like that i 'm using
f.i.value.But i is a number.so i cant use it. How to convert this value of i to string??

function subvalue1(f)
{
var io=f.Count.value;

for(i=1;i<=io;i++)
{
var val=f.i.value;
if(val == &quot;&quot; || val == null){
alert (&quot; Please answer all the Questions&quot;);
f.focus();
return false;
}


}

}
 
Maybe using Eval function?

var val=eval(&quot;f.&quot; + i + &quot;.value&quot;);

or

eval (&quot;val=f.&quot; + i + &quot;.value;&quot;)

(not sure which is correct)

I've not used it much, but it looks as though its worth a peek at.

Julian
 
If you use form element names that begin with a number, Javascript will give you all sorts of errors. Always start names for forms, inputs, radio buttons, dropdown lists, and other form elements with a letter or underscore so Javascript will process them correctly.
 
Thanx julian and Trollacious!!!!!!!!

Priya!
 
For those out there that want to understand what eval does :

This function evaluate a chain of characters as if it was javascript code. Eval is a top level function and is attached to no other objects. It is part of the language itself.

example :

var myString = &quot;alert('John')&quot;
eval(myString) Gary Haran
 
Also you cannot use an attribute like .value in an eval. It just doesn't seem to work properly. It especially doesn't work right if you are setting a form field. The better way is to use eval to get an object (i.e. form field) reference, and then use that to access attributes like .value. Like this:
Code:
// this gets the value from document.myform.myfield2
var num = 2;
var fldref = eval(&quot;document.myform.myfield&quot; + num);
var fldval = fldref.value;
// you can set the value too
fldref.value = &quot;something new&quot;;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top