Web Develop Wolf
Web Develop Wolf

Reputation: 6326

Unable to get value from single radio button in group of radio buttons

I have a yes / no radio button combination that both have the name 'Rigid' and what I'm trying to do is loop over them and grab the the value to match it to a database value. I can get the DB value ok but I don't seem to be able to get the radio button value.

Here's the JQuery I'm using:

var rigid = $("[name='Rigid']");
for (var j = 0; j < rigid.length; j++) {
    var selValueRigid = rigid[j].val();
    if (selValueRigid === data.Rigid) {
        rigid[j].prop("checked", true);
    } else {
        rigid[j].prop("checked", false);
    }
}

And the generated HTML:

<label for="RigidYes">
    <i class="fa fa-check-circle active"></i>
    <i class="fa fa-circle-thin inactive"></i>Yes
</label>
<input name="Rigid" id="RigidNo" value="F" checked="checked" type="radio">
<label for="RigidNo">
    <i class="fa fa-check-circle active"></i>
    <i class="fa fa-circle-thin inactive"></i>No
</label>

Upvotes: 0

Views: 38

Answers (1)

Satpal
Satpal

Reputation: 133403

You can directly use

$('[name="Rigid"][value="' + data.Rigid + '"]').prop('checked', true);

However as per problem statement, use .eq(index) method to get object at specified index.

var el = rigid.eq(j);
el.prop("checked", el.val() === data.Rigid);

Problem with your implementation is that [] will return you reference of native DOM element which doesn't have .val() and .prop() method.

Upvotes: 2

Related Questions