1

I have a script to make a div appear or disappear, depending if a checkbox is checked or not... it works fine.

However, I wish to adapt this script to do the same for radio buttons, depending if one specific value is selected or not.

This would looks like this:

<input type="radio" name="data" id="showDiv" value="1">
<input type="radio" name="data" value="2">
<div id="showMe">Hello</div>

And the JavaScript I wish to modify looks like this:

$('[name="checkbox"]').on('change', function() {
    $('#id').toggle(this.checked);
}).change();

1 Answer 1

1

You can do the same but based on the value.

$('[name="data"]').on('change', function() {
  // toggle based on the value ( value would be the value of checked radio )
  $('#showMe').toggle(this.value == 1);
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="data" id="showDiv" value="1">
<input type="radio" name="data" value="2">
<div id="showMe">Hello</div>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.