JavaScript and form radio buttons

For general web development questions that are not specifically related to CSS HTML Validator. This includes (but is not limited to) general HTML, CSS, Accessibility, JavaScript, and SEO questions.
Post Reply
User avatar
Lou
Rank V - Professional
Posts: 295
Joined: Fri Jul 29, 2005 5:55 pm
Location: CO
Contact:

JavaScript and form radio buttons

Post by Lou »

This looks like a sequencing problem but it works the same on all the browsers I have tried (FF8, IE9, Chrome15.0.874.121, Safari4.0.5, Opera11.11).

To check the user form inputs I have an "onsubmit" that calls a JavaScript. All works well except the script can not see the value set by a set of radio buttons. Input of type Text or hidden are visible in the javascript function, but the input from set of radio buttons is "undefined". If I reduce the radio inputs to a "set" of one radio button then the value is visible in the script - if selected. In any case the action server receives the value of any selected radio button.

I have a test file striped down that shows the problem attached. - {Can't attach the file it is located at http:/ /www.knob{DOT}com/Test/pp/Test.html}

The script currently includes sever Alert boxes to troubleshoot. The commented out switch shows what I want to do if I can see the value of amount_4.

If the problem is not obvious to the casual observer, you can change line 46 to return true so the Action is called. If you change line 58 to your email address MailForm will send you an email with all the name/value pairs.

This is striped down from an "order" form including several donations and a set of 8 Membership levels. In the end it will be sent to PayPal for payment. As shown what I want to do is change the "item name" from Membership to include the membership level in addition to checking other user inputs.
Lou
Say what you will about Sisyphus. He always has work.
User avatar
Lou
Rank V - Professional
Posts: 295
Joined: Fri Jul 29, 2005 5:55 pm
Location: CO
Contact:

Re: JavaScript and form radio buttons

Post by Lou »

Found the problem.
W3C HTML 4.01 Specification wrote:If no radio button in a set sharing the same control name is initially "on", user agent behavior for choosing which control is initially "on" is undefined. Note. Since existing implementations handle this case differently, the current specification differs from RFC 1866 ([RFC1866] section 8.1.2.4), which states:

At all times, exactly one of the radio buttons in a set is checked. If none of the <INPUT> elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.

Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".
I did not want to have a preselected button on (like the pre checked 'sent me advertisement' box) so I did not have one of the radio buttons "on". As a result when submitted nothing was sent.

My answer is to add a Checked "Not now Thank you" radio button to the set. So now that I comply with the standard all is well. dah.
Lou
Say what you will about Sisyphus. He always has work.
User avatar
Albert Wiersch
Site Admin
Posts: 3783
Joined: Sat Dec 11, 2004 9:23 am
Location: Near Dallas, TX
Contact:

Re: JavaScript and form radio buttons

Post by Albert Wiersch »

Hi Lou,

Thanks for posting the solution when you found it.

Would it have helped if CSE HTML Validator generated a warning for radio buttons with no default checked radio button? This sounds like a good check to add.
Albert Wiersch, CSS HTML Validator Developer • Download CSS HTML Validator FREE Trial
User avatar
Lou
Rank V - Professional
Posts: 295
Joined: Fri Jul 29, 2005 5:55 pm
Location: CO
Contact:

Re: JavaScript and form radio buttons

Post by Lou »

Albert Wiersch wrote:Would it have helped if CSE HTML Validator generated a warning for radio buttons with no default checked radio button? This sounds like a good check to add.
Would be useful. After you ask I double checked a couple of books and none said "At all times, exactly one of the radio buttons in a set is checked." In fact the following example code

Code: Select all

<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female<br />
was in one source. I can think of other situation where you would want a radio button "only one checked" but not want a default value; gender, membership level... This may lead others down the path of undefined operation. The answer to my problem was deep in the standard.
Lou
Say what you will about Sisyphus. He always has work.
Post Reply