agone07
agone07

Reputation: 51

Making div appear depending of a radio selection

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();

Upvotes: 1

Views: 22

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

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>

Upvotes: 1

Related Questions