I'm borrowing/adapting this simple html/javascript form set up to put some data in the database. The original code uses text fields in the form, but I'm using radio buttons. The first three steps below are the original, and my question comes after...namely, do I give the radio buttons the same id...Hope this is clear...
Step 1. User enters value into form with id "nick"
<tr>
<td><label>User</label></td>
<td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>
</tr>
Step 2. Value associated with id "nick" assigned to variable using id
var inputUser = $("#nick");
Step 3. getting the value from the variable for insertion into database...
if(inputUser.attr("value")
but if it's two "radio buttons" rather than one "text" field....
<td><label>Interview</label></td>
<td><input type="radio" name="interview" id="nick" value="pass" />Pass</td>
<td><input type="radio" name="interview" id="nick" value="fail" /> Fail</td>
Do I give the radio buttons the same "id" so that it's still like this when I assign the value to the variable...
var inputUser = $("#nick");
so that whichever button is checked will be assigned found in the id "nick"?
Yes, the name attribute (elements inside the tags are called attributes) should be same so that only one radio button is selected (Radio buttons are used for creating yes/no questions).
Only one radio button in a group can be selected at the same time. Note: The radio group must have share the same name (the value of the name attribute) to be treated as a group.
You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.
No, an Id attribute should always be unique. If you're using jQuery (looks like you are), you can select it with $('input[name=interview]');
.
Since you are using jQuery you can easily get the value of the selected radio button by using the :checked selector:
$("input[name=interview]:checked").val()
You should definitely not give more than one element the same ID (that's invalid HTML and will lead to confusing bugs in your JavaScript), but even if that worked it wouldn't help in this case since radio buttons as a group don't have a selected value: you need to determine which one is currently checked and then get its value as shown above. (This is not a problem when getting the value on the webserver when the form is actually submitted, because only the value from the checked radio gets submitted.)
Note also that in your original code where you said inputUser.attr("value")
, you could've said inputUser.val()
.
ID Attribute is unique across the page. You should have different Ids for each radio button. Use below code to get the input value.
var inputUser=$('input:radio[name=interview]:checked').val();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With