I am trying to check a radio button but all I have as a ref to the element is the name attribute and the elements value.
I need some way to check the correct radio where name="accounts\['+i+'\]\[account_status\]\[selected\]" and value="current" in "#form1".
I have the code below but it checks the last radio button.
$('#form1 input[name="accounts\\['+i+'\\]\\[account_status\\]\\[selected\\]"]').attr('checked', true);
Thanks
Answer: Use the Attribute Selector You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.
Just type the name of the element without "<" and ">" characters. For example type P, not <P> if the answer is the <P> element.
The [attribute|=value] selector selects each element with a specified attribute, with a value equal to a specified string (like "en") or starting with that string followed by a hyphen (like "en-us").
You can use this jquery attr() syntax for get data-id attribute value. $("selector"). data("data-textval"); You can use this jquery attr() syntax for get data-textval attribute value.
You seem to have left out the value
part. Because of that, it's trying to check all of them regardless of value. Since only one radiobutton in a group can be checked at a time, only the last is checked in the end.
Add the value on the end.
input[name='something'][value='something']
http://jsfiddle.net/KsJCP/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With