Reputation:
How i can get value of checked radio button in radio button list.Here is my code
<div class="report_type">
<input type='radio' name='choices' value='balance'>Balance Report <br/>
<input type='radio' name='choices' value='invoice'>Invoice Report <br/>
<input type='radio' name='choices' value='payment'>Payment Report <br/>
<input type='radio' name='choices' value='traffic'>Traffic Report <br/>
<input type='radio' name='choices' value='cdr'>CDRs
</div>
Give any help????
Upvotes: 5
Views: 3309
Reputation: 205
Short & clear on ES-2015, no dependencies:
function getValueFromRadioButton( name ){
return [...document.getElementsByName(name)]
.reduce( (rez, btn) => (btn.checked ? btn.value : rez), null)
}
console.log( getValueFromRadioButton('payment') );
<div>
<input type="radio" name="payment" value="offline">
<input type="radio" name="payment" value="online">
<input type="radio" name="payment" value="part" checked>
<input type="radio" name="payment" value="free">
</div>
Upvotes: 0
Reputation: 10885
you can get value any checked radio button by using following code
var val = $("input:radio[name='choices']:checked").val();
alert(val); //give value of checked radio button
Upvotes: 4
Reputation: 114417
$('.report_type input').click(function() {
alert($(this).val())
})
Upvotes: 2
Reputation: 166051
You can use an attribute-equals selector to select the set of radio buttons, and the :checked
selector to select the one that is selected. You can then use the val
method to get the value of that element:
$("input[name='choices']:checked").val();
Alternatively, you could just select all input
elements that are descendants of the div
:
$("div.report_type input:checked").val();
Upvotes: 1