Reputation:
I don't understand why this is not working
I have a radio button Yes or No. Yes uses "id=statut1"
<label class="radio inline">
<input type="radio" name="statut" id="statut1" value="1"<?php if($statut=='1') echo ' checked="checked"'; ?>> Actif
</label>
At some point, I would like to know if the Yes radio button is selected or not. I used this
console.log($('#statut1'));
if($('#statut1').is('checked')){
console.log("inside");
}
When I look into the log, whatever the checked value is (true or false) I never go inside the condition. I can't understand why this is not working. I'm doing the same thing with a checkbox and I have no issue.
Looks like is('checked") doesn't work for radio button...but still I found many subjects here saying it's working.
Thanks for help.
Upvotes: 0
Views: 61
Reputation: 699
I think you missed a colon. Use like this:
$('#statut1').is(':checked')
Upvotes: 3
Reputation: 2232
Here are two solutions:
Using onchange
event you can trigger function.
Vanilla JS:
function myFunction(e) {
console.log(e.target.checked);
}
<label class="radio inline">
<input type="radio" name="statut" id="statut1" value="1" onchange="myFunction(event);"> Actif
</label>
jQuery:
$('#statut1').on('change', e => {
console.log(e.target.checked);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="radio inline">
<input type="radio" name="statut" id="statut1" value="1"> Actif
</label>
$('#statut1').is(':checked')
Upvotes: 0