Get the value of the checked switch in the switch list

How can I get the value of the checked switch in the list of radio buttons. 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 ????

+5
source share
5 answers

you can get the value of any registered switch using the following code

var val = $("input:radio[name='choices']:checked").val();
alert(val);  //give value of checked radio button
+4
source
$('.report_type input').click(function() {
   alert($(this).val())
})
+2
source

, , :checked, , . val, :

$("input[name='choices']:checked").val();

input, div:

$("div.report_type input:checked").val();
+1

Short and clear about 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>
Run codeHide result
0
source

All Articles