I have a javascript like below:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<li>
<input type="radio" class="gal_location" name="gal_location" value="0"> All
</li>
<li>
<input type="radio" name="gal_location" class="gal_location" checked="checked" value="11">Sivasagar</li>
<li>
<input type="radio" name="gal_location" class="gal_location" value="3">Guwahati</li>
<li><input type="radio" name="gal_location" class="gal_location" value="9">Jorhat</li>
<script>
if($('.gal_location').is(':checked')) {
var location_id = $("input:radio[name=gal_location]").val();
}
alert('Location id is '+location_id);
</script>
Now I expect to get an alert of Location id is 11
,but instead I am getting an alert of Location id is 0
Use :checked
to get checkedradio button value
var location_id = $("input:radio[name=gal_location]:checked").val()