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

Radio Button Value not being displayed on SUBMIT

Status
Not open for further replies.

Vem786

Technical User
Dec 2, 2003
52
US
Hi:
I'm a new fella to JS. I tried the below code to have the form display Text & radio button selected in the same page on SUBMIT.
But unfortunately I dont find any luck.

can anyone help??

Thanks!!!


<html>
<head>
<title>Form Data Display</title>
</head>
<body>
<form name=&quot;form1&quot;>
<table>
<TR>
<TR>
<TD align=&quot;left&quot; valign=&quot;middle&quot;><font size=2><b>CB1</b>
<TD align=&quot;left&quot; valign=&quot;middle&quot;><font size=2><b>TEXT1</b>
<TD align=&quot;left&quot; valign=&quot;middle&quot;><font size=2><b>RADIOSELECT</b>
<TR>
<TD align=&quot;left&quot;><input type = &quot;checkbox&quot; name=&quot;cb1&quot; value=&quot;ON&quot; checked=&quot;checked&quot; size =25>
<TD align=&quot;left&quot;><input type = text name=&quot;text1&quot; size =25>
<TD align=&quot;left&quot;><input type = radio name=&quot;radioselect&quot; value=&quot;0&quot;><font size=2><b>YES<b>
<input type = radio name=&quot;radioselect&quot; value=&quot;1&quot;><font size=2><b>NO</b>
</table>
<table>
<TR>
<Td align=left><input type=submit name=&quot;submit&quot; value=&quot;SUBMIT&quot; size=15 onClick=&quot;submitQry();return false;&quot;>
</table>
</CENTER>
</form>
<script language=&quot;javascript&quot;>
<!-- hide me

function submitQry()
{
var the_string1 = document.form1.text1.value;
var the_string2 = document.form1.radioselect.value;
var output_string = '';
if(document.form1.cb1.checked)
{
output_string = output_string + the_string1 +&quot; &quot;+ the_string2;
}
if (output_string == '')
output_string = 'No Data to display!!!';

document.writeln(output_string);
}
// show me -->
</script>
</body>
</html>

 
First of all, you are submitting the form to itself. So after the function is run, the form is immediately submitted. Hence, you never see the result. If you don't want to actually submit the form, you must put 'onSubmit=&quot;return false&quot;' in the form tag. Then you will see the function results.
 
2 b more specific:

<form name=&quot;form1&quot; onSubmit=&quot;submitQry();return false;&quot;>

AND

<input type=submit name=&quot;submit&quot; value=&quot;SUBMIT&quot; size=15>
 
I apologize. I spouted off before reading the entire page.

A radio object is actually an array. You access each element by using the array syntax: radioselect[0] or radioselect[1].

Then to check if it has been selected you test the 'checked' property:

if (documnet.form1.radioselect[0].checked){
//do this
} elseif(documnet.form1.radioselect[1].checked){
//do this
}

or you can loop through the array. You can grab the value like this: document.form1.radioselect[0].value
 
Tired of me yet? Here, try this:


var the_string2;
for (var i=0;i<document.form1.radioselect.length;i++){
if (document.form1.radioselect.checked){
the_string2 = document.form1.radioselect.value;
break;
}
}
 
I tried what you stated but it doesn't work. Because there isn't any change in the assignment of statement to the_string2.

You just have gone through the loop, whereas I directly assigned the document.form1.radioselect.value to the_string2.

I guess there should be another way out!!!

Thanks.
 
Here's something that might help:

Code:
  <script language=&quot;javascript&quot;>
<!-- hide me
function fndsel(){
d = document.form1;
for(i=0;i<d.radioselect.length;i++){
 if(d.radioselect[i].checked == true){
   var retval = d.radioselect[i].value;}}
   return retval;
}   

    function submitQry()
    {
        var the_string1 = document.form1.text1.value;
            var the_string2 = fndsel();
        var output_string = '';
    if(document.form1.cb1.checked)
        {  
            output_string = output_string + the_string1 +&quot; &quot;+ the_string2;
    }
    if (output_string == '') 
        output_string = 'No Data to display!!!';
     
        document.getElementById('output').innerHTML = output_string;
    }
// show me -->
</script>

2b||!2b
 
Ooops...

Need to include this line before your </body> tag also.

<div id=&quot;output&quot;></div>

For your output to write to.

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top