R
Randell_D
This is annoying me and taking me a few hours to resolve so all help
appreciated if you can direct me forward. I'm writing a dynamic
script that will retrieve the value of a form field (an html input
tag) regardless if its a text, password, hidden, select, radio or
checkbox.
I have got the script to work correctly for text/password/hidden and
select but fail when I get to radio/check boxes. Roughly summarised,
the following will correctly alert "text" or "select-one" depending on
the value of "myField". However "undefined" is reported when I ask it
to identify a radio/checkbox. Anyone tell me why and where I am going
wrong?
alert( document.forms['myForm'].elements['myField'].type );
I've written test code here... the code loads and correctly identifes
radiobox name 'a' as a radio but when I click on the hyperlink it
identifies the same input box as "undefined".
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form name="form1" method="post" action="">
<p>test1:<input name="test1" type="text" id="test1"></p>
<p>radio1<input name="a" type="radio" value="1" checked>
radio2<input type="radio" name="a" value="2"></p>
</form>
<hr>
<a href="#" onClick="showValues();">Show Values</a>
<script language="JavaScript">
function readRadioButton(radioObj)
{
var counter=new Number(0);
var limit=radioObj.length;
for( counter=0; counter<limit; ++counter )
{ if( radioObj[counter].checked==true )
{ return radioObj[counter].value; }
}
return false;
}
function showValues()
{
radioObject=f1.a;
alert(radioObject.type);
return true;
}
var DOCUMENT=top.document;
var f1=DOCUMENT.forms['form1'];
var vartmp=new String("");
for(var counter=0; counter<2; ++counter)
{
vartmp+="\n"+f1.elements[counter].type
+"="+f1.elements[counter].name;
}
alert(vartmp);
</script>
</body>
</html>
appreciated if you can direct me forward. I'm writing a dynamic
script that will retrieve the value of a form field (an html input
tag) regardless if its a text, password, hidden, select, radio or
checkbox.
I have got the script to work correctly for text/password/hidden and
select but fail when I get to radio/check boxes. Roughly summarised,
the following will correctly alert "text" or "select-one" depending on
the value of "myField". However "undefined" is reported when I ask it
to identify a radio/checkbox. Anyone tell me why and where I am going
wrong?
alert( document.forms['myForm'].elements['myField'].type );
I've written test code here... the code loads and correctly identifes
radiobox name 'a' as a radio but when I click on the hyperlink it
identifies the same input box as "undefined".
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form name="form1" method="post" action="">
<p>test1:<input name="test1" type="text" id="test1"></p>
<p>radio1<input name="a" type="radio" value="1" checked>
radio2<input type="radio" name="a" value="2"></p>
</form>
<hr>
<a href="#" onClick="showValues();">Show Values</a>
<script language="JavaScript">
function readRadioButton(radioObj)
{
var counter=new Number(0);
var limit=radioObj.length;
for( counter=0; counter<limit; ++counter )
{ if( radioObj[counter].checked==true )
{ return radioObj[counter].value; }
}
return false;
}
function showValues()
{
radioObject=f1.a;
alert(radioObject.type);
return true;
}
var DOCUMENT=top.document;
var f1=DOCUMENT.forms['form1'];
var vartmp=new String("");
for(var counter=0; counter<2; ++counter)
{
vartmp+="\n"+f1.elements[counter].type
+"="+f1.elements[counter].name;
}
alert(vartmp);
</script>
</body>
</html>