Testejo

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.











const mainField = ‘main’; // name of main field
const wrapperEls = document.querySelectorAll(`[wrapper-list]`);
const init = function () {
for (const wrapperEl of wrapperEls) {
if (wrapperEl.getAttribute(‘wrapper-list’).toUpperCase() !== mainField.toUpperCase()) {
wrapperEl.setAttribute(‘style’, ‘display: none’);
}
if(wrapperEl.getAttribute(‘class’).includes(‘mauticform-select’)){
wrapperEl.setAttribute(‘onchange’, “validate()”);
}
wrapperEl.lastElementChild.innerText= ”;
}
}();
function validate() {
const currentEl = this.event.currentTarget;
const selectedValue = this.event.target.value.toUpperCase();
const messageEl = this.event.currentTarget.lastElementChild;
let sectionList = {};
try {
for (const wrapperEl of wrapperEls) {
if(wrapperEl.getAttribute(‘class’).includes(‘mauticform-select’)){
sectionList = wrapperEl.children[1];
}
wrapperEl.lastElementChild.innerText= ”;
messageEl.innerText = ”;
if (wrapperEl.getAttribute(‘wrapper-list’).toUpperCase() !== mainField.toUpperCase()) {
if (wrapperEls[0].children[1].value === ”) {
wrapperEl.setAttribute(‘style’, ‘display: none’);
sectionList.options.selectedIndex = 0;
messageEl.innerText = ‘Please select an item from list(it’s required)’;
messageEl.setAttribute(‘style’, ‘display: block’);
}
if ((!wrapperEl.getAttribute(‘wrapper-list’).toUpperCase().includes(selectedValue) &&
!currentEl.getAttribute(‘wrapper-list’).toUpperCase().includes(wrapperEls[0].children[1].value.toUpperCase()))) { // check if selected field is related with the main field’s option which has been selected
wrapperEl.setAttribute(‘style’, ‘display: none’);
sectionList.options.selectedIndex = 0;
messageEl.innerText = ‘Please select an item from list(it’s required)’;
messageEl.setAttribute(‘style’, ‘display: none’);
}
if (wrapperEl.getAttribute(‘wrapper-list’).toUpperCase() === selectedValue) {
wrapperEl.setAttribute(‘style’, ‘display: block’);
messageEl.innerText = ”;
messageEl.setAttribute(‘style’, ‘display: none’);
} else if (selectedValue === “”) {
messageEl.innerText = ‘Please select an item from list(it’s required)’;
messageEl.setAttribute(‘style’, ‘display: block’);
} else {
console.log(`Dependent field “${selectedValue}” doesn’t exist on the page`);
}
}
}
} catch (e) {
console.log(‘Error-log: ‘, e);
messageEl.setAttribute(‘style’, ‘display: block’);
messageEl.innerText = `Error log ${e}`;
}
}