Skip to content
Advertisement

How to use if..else statement on a radio button with jQuery

I have this field at my form for calculating Value Added Tax:

<form method="post" action="" id="form-product">
    <div class="col-md-4">
        <div class="form-check">
            <input class="form-check-input" type="radio" id="vat" name="vat" value="on">
            <label class="form-check-label">
                Calculate VAT 
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input" type="radio" id="vat" name="vat" value="off" checked>
            <label class="form-check-label">
                Do not calculate VAT
            </label>
        </div>
    </div> 
</form>

Now within jQuery, I want to check that if the radio button is set to the value on, do the calculation.

So how can I do that with jQuery.

I would really appreciate any idea or suggestion from you guys…

Thanks in advance.

Advertisement

Answer

I have added a listener to get the value from the radio button.

$('#form-product input').on('change', function() {
   alert($('input[name=vat]:checked', '#form-product').val()); 
});

Here’s an example:

for more info in Jquery refer to this question

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement