...

View Full Version : Correct syntax for referencing a hyphenated form input radio name in onChange event



duhidunno
08-23-2010, 06:41 AM
Hello,

On a simple form, there is a radio button group with two radio buttons, a text box, and a submit button.
The first radio button is checked(selected) by default.
The text box input has the onChange event handler so that if the text is changed, the second radio button will be checked(selected).
The problem is the input radio name contains a hyphen that breaks the JavaScript and the name can not be changed.
What is the correct syntax for referencing a hyphenated form input radio name in the onChange event?

Here is the form that works correctly with no hyphen in the input radio name:


<form name="myFormGood" action="" onsubmit="return false;">
Good Form with no hyphen in radio button name<br>
<input name="radio1" type="radio" value="4" checked> First Option<br>
<input name="radio1" type="radio" value="5"> Second Option<br><br>
Change the text to select the Second Option<br>
<input type="text" name="text1" onChange="document.myFormGood.radio1[1].checked = 'true';" size="10" value=" "><br><br>
<input type="button" name="submit" value="It Works!">
</form>


Here is the form that breaks the JavaScript because it has a hyphen in the input radio name:


<form name="myFormBad" action="" onsubmit="return false;">
Bad Form with hyphen in radio button name breaks JavaScript<br>
<input name="radio-1" type="radio" value="4" checked> First Option<br>
<input name="radio-1" type="radio" value="5"> Second Option<br><br>
Change the text to select the Second Option. Nothing Happens.<br>
<input type="text" name="text1" onChange="document.myFormBad.radio-1[1].checked = 'true';" size="10" value=" "><br><br>
<input type="button" name="submit" value="It Doesn't Work!">
</form>

Arty Effem
08-23-2010, 09:07 AM
onChange="document.myFormBad[ 'radio-1' ][1].checked = 'true';"

duhidunno
08-23-2010, 03:52 PM
Arty,

Thank you very much. It works brilliantly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum