News:

Looking for documentation? Take a look on our wiki

Main Menu

Make VAT required in Checkout once Invoice is selected

Started by dennisg, February 13, 2025, 15:10:25 PM

Previous topic - Next topic

dennisg

Hi,

I'm trying to have the VAT field, in bill to - checkout area, become required if the document selected is "Invoice" from the relevant drop-down "Select Document".

The code I'm using is:

<script>
jQuery(document).ready(function($) {
    var fieldA = $('select[name="Document]'); // Dropdown field
    var fieldA = $('select[name="VAT"]'); // Field to be required

    function toggleRequired() {
        if (fieldA.val() === 'invoice') {
            fieldB.prop('required', true);
        } else {
            fieldB.prop('required', false);
        }
    }

    fieldA.change(toggleRequired);
    toggleRequired();
});
</script>

and it's inserted in the template overrides within html/com_virtuemart/user/edit_address_userfields.php

Also tested it using field IDs instead of field names.

It doesn't work and I can't figure out what I'm doing wrong...

Can you assist me?

Thanks!

hazael

The selector for fieldA is incorrect: $('select[name="Document]'). You forgot to close the string for name - there is a lack of closing quotation marks. You re-declared fieldA instead of defining fieldB - In the second declaration, var fieldA should be var fieldB. The VAT field is most likely an <input> field, not a <select> - therefore, the correct selector is $('input[name="VAT"]').

<script>
jQuery(document).ready(function($) {
    var fieldA = $('select[name="Document"]');
    var fieldB = $('input[name="VAT"]');
    function toggleRequired() {
        if (fieldA.val() === 'invoice') {
            fieldB.prop('required', true);
        } else {
            fieldB.prop('required', false);
        }
    }
    fieldA.change(toggleRequired);
    toggleRequired();
});
</script>