How to get checked RadioButton Index using jQuery? I need to read it and save to cookies to load status later using code
$('input[name="compression"]')[{Reading from cookies code}].checked = true;
1<input name="compression" type="radio" value="1" checked="checked" />
2<input name="compression" type="radio" value="2" />
3<input name="compression" type="radio" value="3" />
Regards Tomas
This returns the zero-based index of the selected radiobutton
$('input[name=compression]:checked').index()
For the case described in the comment use (which generally is the right syntax to use)
$('input[name=porient]:checked').index('input[name=porient]')
If you want to know why the first one gives wrong results you should read the .index() documentation
If no argument is passed to the .index() method, the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.
In the sample from the question there weren't any "other" siblings but your radiobuttons. Thus it works.
In the sample from the comment the <br />
tag is a sibling of the radiobuttons and thus is on index 1. That's why you think index()
gives the wrong position
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