0

This is my HTML

<tr>
    <td class="g">
    Gender
    </td>
    <td class="g_input">
        <input type="radio" name="gender" id="settings_gender" value="Male" checked />&nbsp;&nbsp;Male &nbsp;&nbsp;&nbsp;
    <input type="radio" name="gender" id="settings_gender" value ="Female" />&nbsp;&nbsp;Female
    </td>
</tr>

This is the javascript

$(document).ready(function () {
    $(".update-basic-info").click(function () {
        var gender = $("#settings_gender").val();

        alert(gender);
    });
});

The output is only showing Male, when i check the female radio button, it still shows male

1
  • 2
    ID's should be unique - that's why - It will only get the first element it finds with that ID Commented Mar 13, 2013 at 19:01

2 Answers 2

4

ID's should be unique on your page. You can do it using a class or just the name attribute

var gender = $('input[name=gender]:checked').val();

or changing the ID to class

<input type="radio" name="gender" class="settings_gender" value="Male" checked />&nbsp;&nbsp;Male &nbsp;&nbsp;&nbsp;
<input type="radio" name="gender" class="settings_gender" value ="Female" />&nbsp;&nbsp;Female

var gender = $(".settings_gender:checked").val();

Working jsFiddle: http://jsfiddle.net/TvVFM/

Sign up to request clarification or add additional context in comments.

Comments

0
$('input[name=gender]').click(function(){
     alert($(this).val());
});

or set the class of both radio buttons to "settings_gender" and try this

$('.settings_gender').click(function(){
     alert($(this).val());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.