tags:

views:

130

answers:

6

i am geting undefined for ans . why? what is wrong?

function submitAnswer()
{

var myForm = document.getElementById('quiz');
var ansVal = myForm.ans.value;
var qnoVal = myForm.qno.value;

alert ("ans="  + ansVal);
alert ("qno = " +qnoVal);
return;
}

<form nam="quiz" id="quiz" >
Yes:
<input type="radio" id="ans"  name="ans" value="1"  />
<br />No:
<input type="radio" id="ans" name="ans" value="0"   />

<input id="qno" type="text" name="qno " value="qqq" />

<input type="button" value="" onClick="submitAnswer(); " />
</form>
+2  A: 

You have two elements with the same ID, causing a name conflict. They're also the same as the name attribute on the same element, which could cause some confusion down the road.

JMP
+4  A: 

Using theForm.inputElement is not standard and can't be guaranteed to work. Instead, you should use document.getElementById, or some other DOM mechanism, to find the input element you want. theForm.elements[name] also works.

You'll also need to fix your element IDs before you can do that - you have two <input type="radio" /> elements with an ID "ans", which is incorrect. IDs must be unique:

<input type="radio" id="ans1"  name="ans" value="1"  />
<input type="radio" id="ans2" name="ans" value="0"   />

<script type="text/javascript">
    var ans1 = document.getElementById('ans1');
    var ans1value = ans1.value;
</script>

Or, get the radio button group as a single element using elements:

<script type="text/javascript">
    var theForm = document.getElementById('quiz');
    var ansValue = theForm.elements['ans'].value;
</script>
Rex M
If the radios have different names, they'll not exclude eachother's selection.
JMP
Spot on, Rex. @unknown, more info here:http://www.w3.org/TR/html401/types.html#type-name
T.J. Crowder
@presleyster: That's right, but there's a big difference between *name* and *id*. Rex was talking about ids, not names.
T.J. Crowder
@presleyster whoops, you're correct. I got a little over-aggressive with the typing. Thanks!
Rex M
A: 

Try:

var ansVal = myForm.ans.checked;

Natkeeran
A: 

This will work:

function submitAnswer() {
    var myForm = document.getElementById('quiz');

    // Set a default value, in case no radio button is selected
    var ansVal = 'default value here';

    var qnoVal = myForm.qno.value;

    // Loop through radio buttons, getting the value of the 
    // one that is checked (selected).
    var radioButtons = myForm.ans;

    for (var i = 0; i < radioButtons.length; i++) {
        if (radioButtons[i].checked) {
            ansVal = radioButtons[i].value;
        }
    }

    alert ("ans="  + ansVal);
    alert ("qno = " +qnoVal);

    return;
}
William Brendel
A: 

this will work too

function submitAnswer()
{

var myForm = document.getElementById('quiz');
var qnoVal = myForm.qno.value;
var ansVal = 'none';

 for( i = 0; i < myForm.ans.length; i++ )
 {
    if( myForm.ans[i].checked == true )
    {
       ansVal  = myForm.ans[i].value;
       break; 
    }
 }

alert ("ans="  + ansVal);
alert ("qno = " +qnoVal);
return;
}
Fredou
A: 

This will work

<html>
<form name="form">
Which one is good?<br>
<input type="radio" name="food" value="Spud" 
checked="checked"> Spud<br>
<input type="radio" name="food" value="Carrot"> Carrot<br>
<input type="submit" onclick="get_radio_value()">
</form>


<script type="text/javascript>
<!--

function get_radio_value()
{
for (var i=0; i < document.form.food.length; i++)
   {
   if (document.form.food[i].checked)
      {
      var rad_val = document.form.food[i].value;
        alert(rad_val);
      }
   }
}

//-->
</script>
</html>
Paul Whelan