Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Correct syntax for referencing a hyphenated form input radio name in onChange event

    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:
    Code:
    <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:
    Code:
    <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>

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Code:
    onChange="document.myFormBad[ 'radio-1' ][1].checked = 'true';"

  • Users who have thanked Arty Effem for this post:

    duhidunno (08-23-2010)

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Arty,

    Thank you very much. It works brilliantly.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •