Determine which radio button the user has selected - html

I know some will ask why i am not using asp.net html controls and others will say that I should use asp.net controls, but my question is about how to do this using plain old html controls.
If I have the following:
<INPUT runat="server" type="radio" name="radio" id="radio0">Radio Button 0
<br>
<INPUT runat="server" type="radio" name="radio" id="radio1">Radio Button 1
where the user selects one of these radio buttons, so it should be a client side selection. Is it possible for me (asp.net at the server) to determine which radio button has been selected when the user submits the form?
I need the server to be able to determine this, so it can do an action based on the radio button selected.

I don't know asp.net but normally you would add a value to your radio buttons.
<input runat="server" type="radio" name="radio" id="radio0" value="0"> Radio Button 0
<br>
<input runat="server" type="radio" name="radio" id="radio1" value="1"> Radio Button 1
And submitting the form would return the selected radio button value for the name attribute parameter (in this case you named it radio).

You can determine which radio button has been selected server-side using the .Checked property.
radio0.Checked;

Related

Radio Button Does Not Unchecked When Clicking On Itself

I'm using Bootstrap 3 and I have a radio button like this:
<input type="radio" name="attr_var" class="minimal">
Add To Attribute
Users can properly click on it for checking it, but it does not unchecked when it's re-clicked.
Basically when it's checked, users can be uncheck it when they re-click on it but this does not workout.
So how to fix this?
Radio buttons are distinct from checkboxes. The user can change options between a set of radio buttons, but it cannot be deselected.
You can refer to this answer for a better explanation as to Why is it impossible to deselect HTML "radio" inputs?
PS. This applies to native HTML radio buttons. You can still use a checkbox for this purpose, or create your own buttons with CSS and Javascript that behave the same way.
Radio buttons can't be unchecked, I think you just need a checkbox which you can check or uncheck.
Please find the reference here:https://getbootstrap.com/docs/5.0/forms/checks-radios/
<input type="checkbox" name="attr_var" class="minimal">
Add To Attribute
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault2" checked>
<label class="form-check-label" for="flexRadioDefault2">
Default checked radio
</label>
</div>
If you want to use just single option to be check or uncheck, use Checkbox instead of Radio button.
For example,
<input type="checkbox" name="attr_var" class="minimal"> Add To Attribute

How do I make it so that when I check one radio button, the other radio button in the same form get unchecked in React?

In the form below, when I click the first radio button then I click the second radio button, both remain checked. The behaviour I want is for when I click on radio button, the other radio button gets unchecked automatically. I am using React.js with styled components.
<form>
<input type="radio"/>
<input type="radio"/>
</form>
Give the same name to all the radio buttons (but different values).
<form>
<input type="radio" name="radio" value="1"/> 1st
<input type="radio" name="radio" value="2"/> 2nd
</form>
You have give each radio button a name property with the same value like "group1". Like so:
<form>
<input type="radio" name="group1"/>
<input type="radio" name="group1"/>
</form>

HTML Radio Button Name / ID's and what it returns?

I'm trying to find out what my form returns from the radio buttons?
I am trying to get it to return back one of two options in my form.
<fieldset id="questRewardType">
<input id="questRewardType" type="radio" name="questRewardType" value="0" checked><font color="#FFFFFF"> Item</font>
<br>
<input id="questRewardType" type="radio" name="questRewardType" value="1"><font color="#FFFFFF"> Currency</font>
</fieldset>
if I was to select the radio button Currency that should return back "Currency"? Am I correct in thinking this?
The value for a radio button set is determined by the value attribute of the checked radio button.
The label (which should be marked up with label not font), is used to tell the user what the radio button means.

How do I make radio buttons outside of forms?

I'm trying to program a dynamic form, so I can't use the normal form tags and stuff. I use normal buttons, JQuery, and AJAX calls to simulate a traditional form. However, I can't figure out how to do radio buttons. Any help?
EDIT: Yeah, I suppose I should have been more specific. I tried doing
<input type="radio" />
and stuff, but:
it lets me select more than one button at a time (which kind of defeats the point of radio buttons)
it won't let me deselect a button after it's pressed!
EDIT 2: The reason I'm not using form tags is that I need multiple submit buttons as well, and the only solution I found to that dilemma was to not use form tags.
Why can't you use the form tag? There is nothing stopping you from doing so. But if you don't want to use the form tag, why not:
<input type='radio' name='test' value='1' checked>
<input type='radio' name='test' value='2'>
<input type='radio' name='test' value='3'>
<input type='radio' name='test' value='4'>
Works fine for me. Demo
Edit:
1: You need to specify a name for the radio group, otherwise each input is considered its own group. Hence why you can select more than one button at a time when using <input type="radio" />. Look at my code above. The radio group is 'test'.
2: Radio buttons are suppose to have a default value. When you create a radio group you should be specifying a default value with the checked attribute. A consequence of this is that you can't deselect a radio button. You can either choose a different value or stick with the default. If you want to be able to deselect, then consider using checkboxes instead. I've updated the example code to reflect this.
If you are able to select more than one radio button, its sounds like your name attributes are not matching. What you want to end up with is something like the following:
<input type="radio" name="group-1" value="something-unique">
<input type="radio" name="group-1" value="something-else-unique">
<input type="radio" name="group-1" value="another-unique-something">
<input type="radio" name="group-2" value="something-unique">
<input type="radio" name="group-2" value="something-else-unique">
<input type="radio" name="group-2" value="another-unique-something">
Note that the name attribute is the same for the group of options, which means that the selections will replace each other.
Also, I haven't had any issues not wrapping radios in form tags, when using them purely with javascript, however if you want to do any html post stuff, I would expect that they are required.
You can try this:
HTML
<div>
<ul>
<li><input type="radio" name="radio" value="value1" checked>Radio Button1</input></li>
<li><input type="radio" name="radio" value="value1">Radio Button2</input></li>
<li><input type="radio" name="radio" value="value1">Radio Button3</input></li>
</ul>
</div>
DEMO
There should not be an input element without a form element. You are not going to get the HTML to respond the way you want it to if you do not use it correctly. Multiple submit buttons would indicate the need for multiple forms.
If, for whatever reason, that does not work, perhaps you should reconsider the format through which you are asking the user to submit information.

html5: Significance of attribute named required in checkbox/radio

On form submission, how could you possibly mark a checkbox/radiobutton as required?
Source of inspiration: Pekka's answer to a question
Required checkboxes are not unusual. Practically every registration form uses some form of the "I have read and accept the User Agreement" checkbox.
If you have Opera handy try out the code below. The form won't submit unless the checkbox is checked.
<!doctype html>
<html>
<head>
<title>html5</title>
</head>
<body>
<h1>html5 test</h1>
<form action="/">
<input type="checkbox" required="required" id="cb" name="cb">
<label for="cb">required checkbox</label>
<input type="submit">
</form>
</body>
</html>
For checkboxes, the best way is probably to pre-select it and set it to disabled. Just kidding.
To ensure one radio button in a group has been selected, either start with a default choice or validate using javascript. There are no HTML-ways to do that because every possible selection is valid.
In html5 there is a required attribute for checkboxes.
They are somehow weird, so let me quote something to explain how they work.
For checkboxes, the required attribute shall only be satisfied when one or more of the checkboxes with that name in that form are checked.
For radio buttons, the required attribute shall only be satisfied when exactly one of the radio buttons in that radio group is checked.
Of course you always have to validate server side because the client can always send you whatever he desires. Just use these methods for better user experience.
I tested required attribute for Radio Buttons today on Firefox 17.0.1 on XP SP2.
It seems to comply with the specification of required attribute for radio buttons/groups. As Firefox prompts "Please select one of these options." for both of the code snippets below:
Either you set required attribute for each of the radio buttons
<input type="radio" name="gender" value="male" required="required" />
<input type="radio" name="gender" value="female" required="required" />
Or Any One of the Radio elements
<input type="radio" name="color" value="blue" />
<input type="radio" name="color" value="red" required="required" />
<input type="radio" name="color" value="green" />
Any comments and updates are welcome.
I just tried it on a radio button in Firefox 4. Adding required to one radio input, then submitting before selecting one, triggers a "Please select one of these options" tooltip.
E.g. this works:
<input type="radio" name="gender" value="m" required />
<input type="radio" name="gender" value="f" />