Reputation: 241
I want to get the value of the radio button selected
HTML
<form id='formTeacherOptions' method='post' target='_self' action='static.php'>
<div id='subjectoptions'>
<input name='radioGrpSbjOption' type='radio' value='0' /> View Subject <br />
<input name='radioGrpSbjOption' type='radio' value='1' /> Update Subject <br />
</div>
<div id='testoption'>
<input name='radioGrpSbjOption' type='radio' value='2' /> Create Test
<div id='testoptionsubjects'> </div>
<div id='testoptiontopic'> </div>
<br />
<input name='radioGrpSbjOption' type='radio' value='3' /> View/Update Test <br />
<input name='radioGrpSbjOption' type='radio' value='4' /> Activated Test <br />
</div>
<div id='reportoptions'>
<input name='radioGrpSbjOption' type='radio' value='5' /> Quiz Report <br />
<input name='radioGrpSbjOption' type='radio' value='6' /> Exam Report <br />
</div>
<button id='' type='button'> Submit </button>
</form>
JS
$('input[name="radioGrpSbjOption"]').bind('change', function (){
alert ($('input[name="radioGrpSbjOption"]').val());
})
The value returned is always 0. What is wrong with my code?
Upvotes: 0
Views: 727
Reputation: 8765
Your code $('input[name="radioGrpSbjOption"]')
find all radios and returns first one value that is 0
. so change you code to this:
$('input[name="radioGrpSbjOption"]').bind('change', function (){
alert ($('input[name="radioGrpSbjOption"]:checked').val());
});
it uses :checked selector to find the selected inputs with name="radioGrpSbjOption"
Upvotes: 1
Reputation: 133453
Use :checked
selector
alert ($('input[name="radioGrpSbjOption"]:checked').val());
Upvotes: 4
Reputation: 18601
$('input[name="radioGrpSbjOption"]').bind('change', function (){
alert ($(this).val());
})
Upvotes: 1