aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes How to check the value of Radio buttons Dynimically Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "How to check the value of Radio buttons Dynimically" Watch "How to check the value of Radio buttons Dynimically" New topic
Author

How to check the value of Radio buttons Dynimically

sonu rai
Greenhorn

Joined: May 23, 2007
Posts: 22
hi,
My problem is, i have 4 RadioButtons i have to check whether the buttons are checked or not(Validation) through JavaScript.

such as
Single<INPUT type="radio" name="mst1">Married<INPUT type="radio" name="mst1">
Single<INPUT type="radio" name="mst2">Married<INPUT type="radio" name="mst2">
Single<INPUT type="radio" name="mst3">Married<INPUT type="radio" name="mst3">
Single<INPUT type="radio" name="mst4">Married<INPUT type="radio" name="mst4">
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you give your buttons ID attributes you'll be able to get a reference to them using document.getElementById(id).

Once you do that, you can read the radio button's 'checked' attribute.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jay Bash
Greenhorn

Joined: Jun 29, 2007
Posts: 5
Hope this will work for you
function submitting()
{
if(document.f.mst1[0].checked)
alert("Single is Selected from mst1");
if(document.f.mst1[1].checked)
alert("Married is selected from mst1");


if(document.f.mst2[0].checked)
alert("Single is Selected from mst2");
if(document.f.mst2[1].checked)
alert("Married is selected from mst2");

if(document.f.mst3[0].checked)
alert("Single is Selected from mst3");
if(document.f.mst3[1].checked)
alert("Married is selected from mst3");

if(document.f.mst4[0].checked)
alert("Single is Selected from mst4");
if(document.f.mst4[1].checked)
alert("Married is selected from mst4");

}
</script>
<form name="f">

Single<INPUT type="radio" name="mst1" >Married<INPUT type="radio" name="mst1" >
Single<INPUT type="radio" name="mst2">Married<INPUT type="radio" name="mst2">
Single<INPUT type="radio" name="mst3">Married<INPUT type="radio" name="mst3">
Single<INPUT type="radio" name="mst4">Married<INPUT type="radio" name="mst4">
add one submit/button and specify onclick method in this case it is submitting() </form>
sonu rai
Greenhorn

Joined: May 23, 2007
Posts: 22
hi
No this can't help me ,The radio buttons name are changed dynamically.

such as
Single<INPUT type="radio" name="mst12" value="single">Married<INPUT type="radio" name="mst1" value="married">
Single<INPUT type="radio" name="mst2" value="single">Married<INPUT type="radio" name="mst2" value="married">

the name of radio buttons mst1,mst2 (1,2) is to be changed dynamically, because the buttons may can increase by adding a row.if the 3rd button comes than what can i do.
if i write
var x = document.myForm.mst3.checked;
it is ok for mst3 suppose 1 button increses than how could i get mst4
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
Why not loop thru the possibilities? I have not tested the following code but it sounds as if you need to do something similar to the following:

The key is to add consistent id attribute values to your radio buttons. If you need to access the buttons for a specific row, then you will need to pass the row index via an event handler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to check the value of Radio buttons Dynimically
 
Similar Threads
Radi Button Validation
setting a page element
Radio Buttons
help with radio buttons
Enable-Diable Radio Buttons