user1283576
user1283576

Reputation:

Get value of checked radio button in radio button list

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

Answers (5)

MaxWall
MaxWall

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

Kashiftufail
Kashiftufail

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

zod
zod

Reputation: 12437

This should help you

http://api.jquery.com/val/

see the last example

Upvotes: 0

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

$('.report_type input').click(function() {
   alert($(this).val())
})

Upvotes: 2

James Allardice
James Allardice

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

Related Questions