Find the radio button with same value as variable, then set it checked

Using javascript, I can accomplish this with the following:

for (var cnt=0; cnt<4; cnt++){
 if (document.form1.txtsty[cnt].value == txtsty){
  document.getElementById('txtsty').selectedIndex = cnt;
  document.form1.txtsty[cnt].checked = 'checked';
  break;
 }
}                                                         

How can I accomplish this same thing using jquery?

The difficulty is in applying the same index value of the radio to the select box, so pretty much the same code structure.

Assuming that the form is identified with a unique identifier, you could use something like this:


$('[name="txtsty"]', '#form1').each(function (value, index) {
    if (value === txtsty) {
        $('#txtsty').prop('selectedIndex', cnt);
        this.prop('checked', 'checked');
        return false;
    }
});

Thanks Paul. Seems like its a bit easier to just leave the javascript as I have it.