View Full Version : how to use radio buttons in a function in javascript?

07-20-2010, 12:30 PM
i tried to run the following code with IE but it gives error as
" document.try.radiogrp is null or not a object."

<script language="javascript">
function fresh()
{ document.write("question 1's options are")
document.write("option 1:"+document.try.radiogrp[0].value+"<br/>")
document.write("option 2:"+document.try.radiogrp[1].value+"<br/>")
document.write("option 3:"+document.try.radiogrp[2].value+"<br/>")

<form name="try" >
<input type="radio" name="radiogrp" id="r1" value="yes">yes
<input type="radio" name="radiogrp" id="r2" value="no">no
<input type="radio" name="radiogrp" id="r3" value="may be">may be
<input type="button" value="begin" onClick="fresh();">
please help me find the error and run it correctly with IE.

07-20-2010, 12:39 PM
document.write can only be called during the initial building of a document, not after it is loaded.
Also try is a reserved word and should not be used as an identifier.

07-20-2010, 12:46 PM
@phil thank you very much.
anyone please tell me what possible changes i'll have to make for the code to be correct.

07-20-2010, 01:02 PM
1. Use something other than "try" as the name for your form
2. Look into using innerHTML (http://www.tizag.com/javascriptT/javascript-innerHTML.php) to update the page contents, rather than document.write()