namu
03-18-2009, 09:25 AM
hello
need urgent help with javascript arrays. I need to store the checked value of the radiobutton into an array and then display the result from that array....
below is the code I have so far...I have created an empty array to store the input from the textbox of the form using the JS insert() function and then used show() function to display the result... but I am unable to display which option the user has selected from the radiobutton...please help.
<html>
<head>
<script language="JavaScript" type="text/javascript">
function checkForm()
{
if (document.registerform.studentname.value == "")
{
alert("Please enter your name");
document.registerform.studentname.focus();
return false;
}
if ((document.registerform.selectgender[0].checked == false) && (document.registerform.selectgender[1].checked == false))
{ alert("Please choose your gender");
return false;
}
}
//declare an empty array to store the input from the form
var inputarray = new Array();
function insert(val){
inputarray[inputarray.length]=val;
}
function show() {
var string="<b>All Elements of the Array:</b><br>";
for(i = 0; i < inputarray.length; i++) {
string =string+inputarray[i]+"<br>";
}
if(inputarray.length > 0)
document.getElementById('paraid').innerHTML = string;
}
</script>
</head>
<body>
<form name = "registerform" action="" method="POST">
<TABLE border="0" width="210">
<TR>
<TH width="58">Name</TH><TD width="136"><input type="text" name="studentname" id="studentname" size="20"></TD>
</TR>
<TR>
<TH width="58">Gender</TH><TD width="136"><INPUT TYPE=RADIO NAME="selectgender" VALUE="male">Male<INPUT TYPE=RADIO NAME="selectgender" VALUE="female">Female</TD>
</TR>
</TABLE>
<br />
<input type="button" value="Submit" name="submitform" " onclick="inputarray = new Array(); insert(registerform.studentname.value);insert(registerform.selectgender.checked);show();"/>
</form>
<hr>
<p id='paraid'></p>
</form>
</body>
</html>
if you look at it in the browser, and type in a name in the name textbox and click submit....it displays the result (which was stored in the inputarray) but the radiobutton doesnt work...gives.."undefined" and should give either Male or Female, depending on what the user selects.
Would appreciate some help.
Thank You
namu
need urgent help with javascript arrays. I need to store the checked value of the radiobutton into an array and then display the result from that array....
below is the code I have so far...I have created an empty array to store the input from the textbox of the form using the JS insert() function and then used show() function to display the result... but I am unable to display which option the user has selected from the radiobutton...please help.
<html>
<head>
<script language="JavaScript" type="text/javascript">
function checkForm()
{
if (document.registerform.studentname.value == "")
{
alert("Please enter your name");
document.registerform.studentname.focus();
return false;
}
if ((document.registerform.selectgender[0].checked == false) && (document.registerform.selectgender[1].checked == false))
{ alert("Please choose your gender");
return false;
}
}
//declare an empty array to store the input from the form
var inputarray = new Array();
function insert(val){
inputarray[inputarray.length]=val;
}
function show() {
var string="<b>All Elements of the Array:</b><br>";
for(i = 0; i < inputarray.length; i++) {
string =string+inputarray[i]+"<br>";
}
if(inputarray.length > 0)
document.getElementById('paraid').innerHTML = string;
}
</script>
</head>
<body>
<form name = "registerform" action="" method="POST">
<TABLE border="0" width="210">
<TR>
<TH width="58">Name</TH><TD width="136"><input type="text" name="studentname" id="studentname" size="20"></TD>
</TR>
<TR>
<TH width="58">Gender</TH><TD width="136"><INPUT TYPE=RADIO NAME="selectgender" VALUE="male">Male<INPUT TYPE=RADIO NAME="selectgender" VALUE="female">Female</TD>
</TR>
</TABLE>
<br />
<input type="button" value="Submit" name="submitform" " onclick="inputarray = new Array(); insert(registerform.studentname.value);insert(registerform.selectgender.checked);show();"/>
</form>
<hr>
<p id='paraid'></p>
</form>
</body>
</html>
if you look at it in the browser, and type in a name in the name textbox and click submit....it displays the result (which was stored in the inputarray) but the radiobutton doesnt work...gives.."undefined" and should give either Male or Female, depending on what the user selects.
Would appreciate some help.
Thank You
namu