Leahcim Leahcim - 5 months ago 10
Javascript Question

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

Answer

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