3

How can I determine which radio button is checked on click of a radio button using the Bootstrap Button plugin?

The following is evaluating to false no matter which radio button is selected:

HTML

<div class="btn-group" data-toggle="buttons" id="disabled-radio-btn">
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-yes">Yes
    </label>
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-no">No
    </label>
</div>

JS

$('#disabled-radio-btn').click(function (e) {
    if ($('#disabled-no:checked').length) {
        $('#SSDI').hide();
    }
    else {
        $('#SSDI').show();
    }
});

2 Answers 2

2

should be $('#disabled-no').is(':checked') in the if statement to check if the radio button is checked.

$('#disabled-radio-btn').click(function (e) {

    if ($('#disabled-no').is(':checked')) {
        $('#SSDI').hide();
    }
    else {
        $('#SSDI').show();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group" data-toggle="buttons" id="disabled-radio-btn">
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-yes">Yes
    </label>
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-no">No
    </label>
</div>

<div id="SSDI">SSDI THING</div>

update: https://jsfiddle.net/7hrbcmnb/2/

use event delegation when declaring the click event

$(document).on('click', '#disabled-radio-btn', function (e) {});

Sign up to request clarification or add additional context in comments.

5 Comments

Also note that you can reduce the if statement to just $('#SSDI.toggle(!$('#disabled-no').is(':checked'));
@indubitablee Thanks, however this isn't my issue. Somehow Bootstrap is effecting my click events. On first click, both radio buttons are evaluating to false. See fiddle here: jsfiddle.net/7hrbcmnb
@noclist, i updated my answer with the scenario in your jsfiddle
@indubitablee perfect, thank you. so, we need event delegation here because the bootstrap plugin script is in a different scope than my own, correct?
@noclist yup, thats what i think. also note Rory's above comment if you want to simplify the if statement. his suggestion is quite elegant.
1

Use the val() method with the checked attribute.

if($("#disabled-no:checked").val())

$('#disabled-radio-btn').click(function (e) {

    if ($('#disabled-no:checked').val()) {
        $('#SSDI').hide();
    }
    else {
        $('#SSDI').show();
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group" data-toggle="buttons" id="disabled-radio-btn">
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-yes">Yes
    </label>
    <label class="btn btn-default">
        <input type="radio" name="disabled" id="disabled-no">No
    </label>
</div>

<div id="SSDI">SSDI THING</div>

1 Comment

Thank you, the issue I'm having is different. See fiddle here: jsfiddle.net/7hrbcmnb The bootstrap button plugin is effecting my initial click.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.