My site is www.out4biz.ca
I would like to get these custom attributes working on Listings - when transaction_type “barter” or “both” is selected, show “barter_details” and hide “price” and “hourly_rate”. When transaction_type “paid” is selected, hide “barter_details”.
I am using this code snippet but it’s not working:
add_action('wp_footer', function () {
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
function toggleTransactionFields(form) {
const typeField = form.querySelector('[name*="transaction_type"]');
const barterField = form.querySelector('[name*="barter_details"]')?.closest('.hp-field') || form.querySelector('.hp-field--barter-details');
const priceField = form.querySelector('[name*="price"]')?.closest('.hp-field') || form.querySelector('.hp-field--price');
const rateField = form.querySelector('[name*="hourly_rate"]')?.closest('.hp-field') || form.querySelector('.hp-field--hourly-rate');
if (!typeField) return;
const selected = typeField.value.toLowerCase();
if (selected === 'barter' || selected === 'both') {
if (barterField) barterField.style.display = '';
if (priceField) priceField.style.display = 'none';
if (rateField) rateField.style.display = 'none';
} else if (selected === 'paid') {
if (barterField) barterField.style.display = 'none';
if (priceField) priceField.style.display = '';
if (rateField) rateField.style.display = '';
} else {
if (barterField) barterField.style.display = '';
if (priceField) priceField.style.display = '';
if (rateField) rateField.style.display = '';
}
}
function initAllForms() {
const allForms = document.querySelectorAll('form');
allForms.forEach(form => {
const typeField = form.querySelector('[name*="transaction_type"]');
if (typeField) {
toggleTransactionFields(form);
typeField.addEventListener('change', function () {
toggleTransactionFields(form);
});
}
});
}
initAllForms();
});
</script>
<?php
});