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
  • 24,408
  • 27
  • 85
  • 150
  • 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
  • 34,369
  • 22
  • 118
  • 164
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/

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