bba bba - 6 months ago 8
Javascript Question

Checking a radio button in JQuery

I need to problematically (do you mean programmatically?) check a radio button given its value. The form has an

id
and the input type obviously has a name (but no id). The only code I managed to get working so far is:

$('input[name=my_name]:eq(1)').attr('checked', 'checked');


But I'd like to be able to check it by explicitly providing the value.

Answer

So you want to select the radio which has a particular value:

$('input[name=my_name][value=123]').attr('checked', true); // or 'checked'