Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two radio buttons share one "id"?

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"?

like image 221
Leahcim Avatar asked Nov 29 '11 04:11

Leahcim


People also ask

Can two radio buttons have same ID?

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).

Can you have multiple radio buttons?

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.

How do you group radio buttons together?

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.


3 Answers

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]');.

like image 92
Matthew Avatar answered Oct 12 '22 23:10

Matthew


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().

like image 42
nnnnnn Avatar answered Oct 12 '22 22:10

nnnnnn


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();
like image 44
Selvakumar Ponnusamy Avatar answered Oct 13 '22 00:10

Selvakumar Ponnusamy