Click to See Complete Forum and Search --> : Radio buttons in a datagrid


S. Go
12-05-2003, 04:23 PM
I have a data grid with each rows displaying a record retrieved from the database.

One column of the data grid contains radio buttons the number of which depends on the entries in a look up table (for example: radio button Model 1, Model 2 .... up to the last entries in the look up table). I name each group of radio buttons in a row according to the row number. For example:

On the first row the radio buttons are:
<input type=radio name=row1, value="Model1">Model 1
<input type=radio name=row1, value="Model2">Model 2
.......

On the second row, the radio buttons are:
<input type=radio name=row2, value="Model1">Model 1
<input type=radio name=row2, value="Model2">Model 2
.......

Questions:
How can I go through each row and find out which button (on that row) has been checked? I want to put the result in an array with the selected name/value pair.

Appreciate your response. Thanks very much in advance.

sg2000

David Harrison
12-05-2003, 04:55 PM
This script runs a loop to find out which radio button is checked, when (or if) it finds one it alerts the value of that radio button.

limit=document.the_form.row1.length;

for(n=0;n<limit;n++){if(document.the_form.row1[n].checked){break;}}

if(n<limit){alert(document.the_form.row1[n].value);}

S. Go
12-06-2003, 11:05 AM
Lavalamp:

Thanks very much for the quick response. Your method solves the problem of checking one set of radio buttons. In my case, however, my data grid has a set of radio buttons on each row of the grid. The number of rows is dynamic (equals to the number of records retrieved from the database). Each set of buttons on a row has different name (e.g. Row 1, Row2, Row 3 etc.). What I would like to know is how to go through each row of radio buttons and compile an array with the pair of Row[i]/Value[i]. iHopefully you can help me in this respect.

Thanks again.

sg2000

David Harrison
12-06-2003, 12:38 PM
Stick an eval in the works and look what happens:

var limit, rad;
var max_row_num=3;

for(o=1;o<max_row_num+1;o++){

eval('rad=document.the_form.row'+o+';limit=rad.length;');

for(n=0;n<limit;n++){if(rad[n].checked){break;}}

if(n<limit){alert(rad[n].value);}}

S. Go
12-06-2003, 08:58 PM
Lavalamp:

Thanks again for the quick response. Can you tell me what is the function of eval in the
eval('rad=document.the_form.row'+o+';limit=rad.length;')?

Thank very much.

sg2000

David Harrison
12-06-2003, 09:39 PM
eval is just a way of telling the computer to evaluate whatever is inside the brackets as JavaScript code. But because of the quotation marks it allows me to import variables, such as the row number, so that when it runs the line of code the row number can change due to the for loop.

fredmv
12-06-2003, 10:32 PM
Why are you using eval? It's almost never needed (the only time I've seen it needed is some scoping issues). In this particaular case, using the elements array is the best solution as it uses less resources and allows you to do the exact same thing. For example:document.the_form['row' + o]

David Harrison
12-07-2003, 08:14 AM
Wouldn't this:

document.the_form['row' + o]

only be useful if each row was in a different form? Maybe not, I don't tend to use arrays much unless I create them in the script itself so I don't know that much about them. The only reason I used them here was because I couldn't think of another way of doing it. Perhaps I'll go away and read up on how I can use arrays to refer to HTML elements.

S. Go
12-07-2003, 10:08 AM
Lavalamp:

Thank you very much for your response. I got it working using Eval as you suggested. Everything is OK now. I have not tried the method suggested by Fredmv.

Thanks again for your tremendous assistance.

sg2000

David Harrison
12-07-2003, 01:42 PM
Happy to help. :)

Personally I don't see anything wrong with using eval so you should have no problems with it.