Hide/Show Based on Radio Button Click

Tag: if-statement , click , hide , show , radio Author: jactaohai Date: 2013-04-13

I've been searching for a few hours now and haven't been able to get an aspect of my code working.

I have two radio buttons for a div, and I want it so if I select Yes, it shows one slider, if I selected No, it shows another.

Below is the .html:

<fieldset data-role="controlgroup">
    <legend>
        <h>Do you like pie?</h>
    </legend>
    <input type="radio" name="do-you-like-pie" id="do-you-like-pie-yes" value="do-you-like-pie-yes" checked="checked" />
    <label for="do-you-like-pie-yes">Yes</label>
    <input type="radio" name="do-you-like-pie" id="do-you-like-pie-no" value="do-you-like-pie-no" />
    <label for="do-you-like-pie-no">No</label>
</fieldset>
<!-- first slider -->
<div data-role="fieldcontain" class="show-hide-like-pie-no">
    <label for="like-pie-yes-slider">
        <h>How much?</h>
    </label>
    <input type="range" name="like-pie-yes-slider" id="like-pie-yes-slider" value="25" min="0" max="100" data-highlight="true" />
</div>
<!-- 2nd slider -->
<div data-role="fieldcontain" class="show-hide-like-pie-yes">
    <label for="like-pie-no-slider">
        <h>How litle?</h>
    </label>
    <input type="range" name="like-pie-no-slider" id="like-pie-no-slider" value="100" min="0" max="200" data-highlight="true" />
</div>

Below is the .js:

function getAuto() {
    if ($('#do-you-like-pie-yes').is(':checked')) {
        return true;
    } else {
        return false;
    }
}

//$('do-you-like-pie').on('click',function () {
//   if ($('#do-you-like-pie-yes').is(':checked')) {
if (getAuto() == "true") {
    $(".show-hide-like-pie-yes").show();
    $(".show-hide-like-pie-no").hide();
} else {
    $(".show-hide-like-pie-yes").hide();
    $(".show-hide-like-pie-no").show();
}
//});

Any help would be greatly appreciated!

Here is the fiddle link: http://jsfiddle.net/DjT5X/

Thanks!

Kind Regards,

Gary

edit: the trouble with other solutions is that they don't use an "if"

So I have a solution: jsfiddle.net/DjT5X/1 But there must be a way to tidy this up? Thanks

Other Answer1

The getAuto() returns a boolean true/false. In your example you the test for the string "true"

Just use the simple if (getAuto()) instead

//$('do-you-like-pie').on('click',function () {
//   if ($('#do-you-like-pie-yes').is(':checked')) {
if (getAuto()) {
    $(".show-hide-like-pie-yes").show();
    $(".show-hide-like-pie-no").hide();
} else {
    $(".show-hide-like-pie-yes").hide();
    $(".show-hide-like-pie-no").show();
}
//});

comments:

Hi, thanks for the reply. Unfortunately, that only works on "Run", it doesn't change when I click. The hope is that when I click Yes, it shows slider 1, when I click No, it shows slider 2. Thanks.

Other Answer2

Sample example to hide button on radio button click

  $(document).ready(function() {
    $("#do-you-like-pie-yes").click(function() {
    $(".show-hide-like-pie-no").hide();
    });
    });

Other Answer3

Fixed - Just used change(function())