Leahcim Leahcim - 1 year ago 70
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"

<td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>

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


but if it's two "radio buttons" rather than one "text" field....

<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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download