0

How can I do a simple if statement based on radio button selection?

So if radio button inches is displayed, then div.inchesUser is shown (using .show) And the opposite for centimetres.

This is what I want the function to do:

$('div.inchesUser').show(200);

This is my radio button code:

<form id="unitChooser">
    <label>Inches or centimetres?</label>
    <input class="unitSelectInches" type="radio" name="units" value="inches" />Inches
    <input class="unitSelectCenti" checked="checked" type="radio" name="units" value="cms" />Centimetres
</form>
Francesca
  • 26,842
  • 28
  • 90
  • 153
  • possible duplicate of [How can I get which radio is selected via jQuery?](http://stackoverflow.com/questions/596351/how-can-i-get-which-radio-is-selected-via-jquery) – Barmar Feb 17 '14 at 20:31

2 Answers2

0

try this

$("input:radio[name=units]").click(function() {
    var value = $(this).val();
    if(value == 'inches')
        $('div.centimetreUser').show(200);
});

DEMO

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
0

Something like this -

<form id="unitChooser">
    <label>Inches or centimetres?</label>
    <input class="unitSelectInches" data-class='inchesUser' type="radio" name="units" value="inches" />Inches
    <input class="unitSelectCenti" data-class='centimetresUser' checked="checked" type="radio" name="units" value="cms" />Centimetres
</form>

$('input:radio[name=units]').on('change', function () {
    $('div.centimetreUser, div.inchesUser').hide();
    $('div.' + $(this).data('class')).show(200);
});

Demo ---> http://jsfiddle.net/WT2uc/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • @Francesca See this demo `-->` http://jsfiddle.net/WT2uc/ ------ your fiddle updated `-->` http://jsfiddle.net/8pEp2/12/ – Adil Shaikh Feb 17 '14 at 20:42