Rushabh Shah
Rushabh Shah

Reputation: 515

How to get value of checked radio?

HTML:

<div id="chips">
    <label>
        <input type="radio" name="pet_chipped" class="chipped" value="Yes" id="chipyes" checked> Yes</label>
    <label>
        <input type="radio" name="pet_chipped" class="chipped" value="No" id="chipno"> No</label>
</div>

jQuery:

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

when I am alerting 'pet_chipped' it displaying undefined.

Upvotes: 0

Views: 48

Answers (2)

Suresh Suthar
Suresh Suthar

Reputation: 812

//onload event
$(document).ready(function(){
  /*show alert on load time*/
  alert($('[name="pet_chipped"]:checked').val());
})
// on change radio button value fire event 
$(document).on('change', '[name="pet_chipped"]', function(){
  //show value of radio after changed
   alert($('[name="pet_chipped"]:checked').val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="chips">
<label> <input type="radio" name="pet_chipped" class="chipped" value="Yes" id="chipyes" checked> Yes</label>
<label><input type="radio" name="pet_chipped" class="chipped" value="No" id="chipno"> No</label>
</div>

Upvotes: 2

Hari
Hari

Reputation: 44

Try this

HTML :

<input type="radio" name="pet_chipped" class="chipped" value="Yes">    Yes</label>
<label>
<input type="radio" name="pet_chipped" class="chipped" value="No" checked> No</label>
<input type="submit" onclick="getChecked()" />

Jquery :

<script type="text/javascript">
function getChecked(){
var checked = $("input[name='pet_chipped']:checked").val();
alert(checked);
}
</script>

Upvotes: 0

Related Questions