Click to See Complete Forum and Search --> : (Simple) Short syntax problem


LocalHero
05-30-2005, 09:07 PM
I thought I knew what I was doing, but I can't figure out how to make this JS work:

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT type="text/javascript">
function size() {
if (document.forms.form1.radiobutton.selected==Height) {document.forms.textfield.value="Height"};
if (document.forms.form1.radiobutton.selected==Width) {document.forms.textfield.value="Width"};
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="form1" method="post" action="">
<INPUT name="radiobutton" type="radio" value="Height" onFocus="size();">
Height&nbsp;&nbsp; <INPUT name="radiobutton" type="radio" value="Width" onFocus="size();">
Width
<INPUT type="text" name="textfield">
</FORM>
</BODY>
</HTML>

I just want to display either "width" or "height" in the box. I thought I knew how to do it with checkboxes, but I'm missing something.

LocalHero
05-30-2005, 09:41 PM
I've fixed the function to this:

function sizea() {
if (document.forms.form1.radiobutton.value = "Height") {document.forms.form1.textfield.value="Height"};
if (document.forms.form1.radiobutton.value = "Width") {document.forms.form1.textfield.value="Width"};
}

But it only displays "width" no matter what is selected.

BigMoosie
05-30-2005, 09:43 PM
You had several problems, first I noticed that you were checking your radiobuttons selected incorrectly, then you were using size as a function but that is a reserved keyword so I changed it to size1, also you forgot to refer to which form after the if statements. Finally you were calling the function onfocus rather than onclick which caused the function to be called prematurally.

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT type="text/javascript">
function size1() {
if (document.forms.form1.radiobutton[0].checked) {document.forms.form1.textfield.value="Height"};
if (document.forms.form1.radiobutton[1].checked) {document.forms.form1.textfield.value="Width"};
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="form1" method="post" action="">
<INPUT name="radiobutton" type="radio" value="Height" onclick="size1();">
Height&nbsp;&nbsp; <INPUT name="radiobutton" type="radio" value="Width" onclick="size1();">
Width
<INPUT type="text" name="textfield">
</FORM>
</BODY>
</HTML>

LocalHero
05-30-2005, 09:54 PM
Thanks again!