Setting value of RadioButtonFor using JavaScript

I just saw an implementation of RadioButtonFor with true orfalse as 2nd parameter, but how do I set the checked value using JavaScript?

I already have an existing code that sets the checked value of a radiobutton via Id in JavaScript and only using @Html.RadioButton in View. But what I want now is to use RadioButtonFor. How do I do this? My active and inactive has the same Id, both rbtn only differs in value.

PS: Not working code is at the bottom part of the JavaScript

Model:

[Display(Name = "Status")]
public string userStatus { get; set; }

View:

@Html.LabelFor(m => m.userStatus)
@Html.LabelFor(m => m.userStatus, "Active")
@Html.RadioButtonFor(m => m.userStatus, true)
@Html.LabelFor(m => m.userStatus, "Inactive")
@Html.RadioButtonFor(m => m.userStatus, false)

part of Javascript:

//columnData - represents the column index in my table
//fieldId - id of the fields (e.g. text = userDesc, radio = userStatus)

for (i = 0; i < columnData.length; i++) {
    //Evaluates what type an input is
    var elmntType = document.getElementById(fieldId[i]).getAttribute("type");

    if (elmntType == "text") {

        document.getElementById(fieldId[i]).value = rowSelected.cells[i].innerHTML.trim();

    } else if (elmntType == "radio") {

        var status = rowSelected.cells[i].innerHTML.trim();

        //I just invented this but it's not working
        if (status == "Active") {
            document.getElementById(fieldId[i]).checked = true;
        } else {
            document.getElementById(fieldId[i]).checked = false;
        }
    }               
}

Upvotes: 0

Views: 906

Answers (0)

Related Questions