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!
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>