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

Find out if radio button is checked

Status
Not open for further replies.

robi2

Programmer
Jan 19, 2003
62
IL
Hi, how can i find out if a radio button has been checked or not.

I know this way:

window.document.myform.myradio[0].checked
And it works fine
But this seems to give me an error:

window.document.forms[0].elements[0][0].checked

Any suggestions?

Thanks.
 
thread216-450209

hint
.checked == true _______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
I didnt mean that!
I'm not a complete noob.

Please try running it and see if it works or not.
 
[lol] that was good
but
running what?
how can i find out if a radio button has been checked or not

in the input put onClick="itwaschecked(this)"
or something
function itwaschecked(obj) {
// whatever to the event it was clicked
}
then run whtever you want on that event

btw: window and document are their own objects
window.document is wrong
either
window.
or
document.
you can't try and access two elements
elements[0][0].checked
that's why the error comes up

_______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
running = see if it works properly in your browser
btw window.document does work.

So how can i see if the radio button is checked?

here's an example:
var path = window.document.myform
for (i=0;i<path.elements.length;i++)
{
if (path.elements[0].checked == true)
{alert('YEEAAAAHHAAAA'))
}

Asuming all form elements are radios...

But still this will return an error.
 
I'll stick with the window thing but
<html>
<head>
<script language=&quot;JavaScript&quot;>
function testit() {
var path = document.myform
for (x=0;x<path.elements.length;x++) {
if(path.elements[x].checked == true) {
alert('YEEAAAAHHAAAA')
}
}
}
</script>
</head>
<body>
<form name=&quot;myform&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;button&quot; value=&quot;see&quot; onClick=&quot;testit()&quot;>
</form>
<body>
<form name=&quot;myform&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;checkbox&quot;>
<input type=&quot;button&quot; value=&quot;see&quot; onClick=&quot;testit()&quot;>
</form> _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
copy/paste got me that's only one set of checkbox's and one form _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
You gave an exaple of check boxes and not of radio buttons...
 
wow, but OK I'll play
<html>
<head>
<script language=&quot;JavaScript&quot;>
function testit() {
var path = document.myform
for (x=0;x<path.elements.length;x++) {
if(path.elements[x].checked) {
alert('YEEAAAAHHAAAA')
}
}
}
</script>
</head>
<body>
<form name=&quot;myform&quot;>
<input type=&quot;radio&quot; name=&quot;k&quot;>
<input type=&quot;radio&quot; name=&quot;k1&quot;>
<input type=&quot;radio&quot; name=&quot;k2&quot;>
<input type=&quot;radio&quot; name=&quot;k3&quot;>
<input type=&quot;radio&quot; name=&quot;k4&quot;>
<input type=&quot;radio&quot; name=&quot;k5&quot;>
<input type=&quot;button&quot; value=&quot;see&quot; onClick=&quot;testit()&quot;>
</form> _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
What if there are moer radio buttons under the same name, how do you divide them?

I mean:
<input type=&quot;radio&quot; name=&quot;k&quot;>
<input type=&quot;radio&quot; name=&quot;k&quot;>
<input type=&quot;radio&quot; name=&quot;k&quot;>

So what if i want to check if any radio button under the same group was checked?

Well i just figured it out.

i just do:

window.document.forms[0].elements[0,0].checked

instead of:

window.document.forms[0].elements[0][0].checked


Thanks for all the help.
 
[thumpsup]

remember the incrementing of the counter in a loop will take a bit more logic to test for a group of radios or checkboxs (same thign basically) _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
on the window document object referencing
it really isn't needed to add window.document
document is a child object of the window object but not normally referenced as such. I believe the need for that ws taken away in like NN 1 [lol] _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
look try this

for(var i =0;i<document.myform.myradio.length;i++)
{
if(document.myform.myradio == checked)
{
window.alert(&quot;Checked&quot;);
}

}
 
look try this

for(var i =0;i<document.myform.myradio.length - 1;i++)
{
if(document.myform.myradio == checked)
{
window.alert(&quot;Checked&quot;);
}

}
 
I was aiming to not ahve to write the names manually, so i can do it in a loop using the forms and elements collections. Thanks but i already found my answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top