Reputation: 515
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
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
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