neha910 neha910 - 1 month ago 8
HTML Question

How to Check/Uncheck single radio button

$('.ssn_byphone').click(function(){
var ssn_role = document.getElementById('ssn_byphone').checked;
if(ssn_role==true)
{
$('#ssn-div').hide();
$('.ssn_byphone').css('display','block');
$('#ssn').val('');
}
else
{
$('#slideUp').show();
}
});

<input id="ssn_byphone" class="ssn_byphone" type="radio" name="pi[ssn_byphone]" value="Yes"><label for="radio1"> I will provide SSN by phone</label>


I want to check/uncheck this radio button when i clicked on it, and want to set condition accordingly.

Answer

Try this:

var checked=false;
$('.ssn_byphone').click(function(){
    checked=!checked;
    $(this).prop("checked",checked);
    if($(this).is(':checked'))
    {
        $('#ssn-div').hide();
        $('.ssn_byphone').css('display','block');
        $('#ssn').val('');
    }
    else
    {
        $('#slideUp').show();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<input id="ssn_byphone" class="ssn_byphone" type="radio" name="pi[ssn_byphone]" value="Yes"><label for="radio1"> I will provide SSN by phone</label>

Comments