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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of selected radio buttons on a form

Status
Not open for further replies.

fgeorge

Programmer
Jun 28, 2002
76
NG
I have several groups of radio buttons on a form how can i loop through all thge radio buttons and determine which ones are selected.
 
Code:
    var el = document.getElementsByTagName("input");

    for( i=0; i < el.length; i++ ) {

        if ( el[i].checked ) {

		// Some code to run
		// if it's checked
        }
    }
 
thanks for the post i will try it out now. but on which event would i put it .
what i am actually trying to achive is so that when a page is refreshed i determine which radio buttons are selected and assign a colour to them in another cell...

the issue i had before when i put the color function On Click was that when the page was refreshed the selected radio buttons show but with out the colours.

i hope i am making a bit of sense here.
thanks
 
tope,
u cant maintain state between page refreshes..
pass the variables to a different but similar page..
 
Okay, you said that's it's remembering the selected radio's on refresh, which is the behaviour I'd expect too. So, you'd need to run your code &quot;onload&quot;.

HTH.
 
but onload will take the page back to its initial state..
right?

it cant &quot;remember&quot; which radio buttons were selected..
 
Well, I created a page that initially has none selected, selected a few, and refreshed the page. They were still selected and the script worked.

That's all the testing I bothered with though. :)
 
i tried this and i get an error that it can't find length or something ...... the functions that change 'the colours in the first place are in the header of the main document.At runtime i pass a temporary value to it so that it displays the color i need .(these are the functions :

function ColorCode(tmpval){
var tmp;
var idx = parseInt(tmpval);

switch (idx) {
case 1 : tmp = &quot;#cccc66&quot;; break;
case 2 : tmp = &quot;#cc66ff&quot;; break;
case 3 : tmp = &quot;#6666ff&quot;; break;
case 4 : tmp = &quot;#00ffff&quot;; break;
case 5 : tmp = &quot;#ff6633&quot;; break;
default : tmp = &quot;#coe1ff&quot;; //&quot;white&quot;;
}
return tmp;
}

function display(tmpval){

var str = tmpval.toString().substring(0,3);
var val = Score(eval(&quot;document.forms[0].&quot; + tmpval));
eval(str +&quot;2&quot;).innerText = val;
eval(&quot;document.all.&quot;+str+&quot;6&quot;).bgColor = ColorCode(val); //paints the score cell with color corresponding to the selected value
}
else{

//displays the value in a cell e.g s312
eval(&quot;document.all.&quot;+str+&quot;7&quot;).bgColor = ColorCode(val); //paints the score cell with color corresponding to the selected value
}
})
hope it makes a bit of sense...
at runtime i then call function directly on each field on the OnClick event .
e.g display(tmpval) = display (s311)
but when i refresh the form or call it up again , i see the checked radio button but i dont see the colours or values anymore....

any ideas's , please help....(femi mind ur own $£&quot;)
 
Quote: &quot;I tried this and i get an error that it can't find length or something ...&quot;

Surely you can give us a better description than that!!
 
given the below code,
for initial radio button form :
<INPUT
onclick=&quot;display('s311');&#13;&quot; type=radio value=1 name=s311>1<BR><INPUT
onclick=&quot;display('s311');&#13;&quot; type=radio value=2 name=s311>2<BR><INPUT
onclick=&quot;display('s311');&#13;&quot; type=radio value=3 name=s311>3<BR><INPUT
onclick=&quot;display('s311');&#13;&quot; type=radio value=4 name=s311>4<BR><INPUT
onclick=&quot;display('s311');&#13;&quot; type=radio value=5
name=s311>5</FONT></DIV><BR></TD>


after the form has been filled and saved...

<INPUT type=hidden value=2 name=s311> <INPUT type=hidden
value=2 name=s321> <INPUT type=hidden value=2 name=s331> <INPUT type=hidden
value=5 name=s341> <INPUT type=hidden value=2 name=s351> <INPUT type=hidden
value=4 name=s361> <INPUT type=hidden value=3 name=s371>

how would i directly reference the value 2 of s311 for instance. i need this value . so that insted of cycling through thr radio buttons i can use the submited form to reference the values directly and use the value to make the clours on the form permanent ?.. what do u think. how can i reference the value directly from the form ...
 
I don't think I understand the question anymore. :0/

However, to get the value of an element with name s311 you could use
Code:
document.getElementsByName(&quot;s311&quot;).item(0).value
.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top