(function () { function setupForm() { const kommer = document.querySelector('[name="kommer-du"]'); const antall = document.querySelector('[name="antall-personer"]'); const allergiSelv = document.querySelector( '[name="allergier-spesielle-matbehov-for-deg-selv"]' ); const folge = document.querySelector('[name="navn-p-flge"]'); const allergiFolge = document.querySelector( '[name="allergier-spesielle-matbehov-for-ditt-flge"]' ); /* Vent til Carrd har laget skjemaet */ if (!kommer || !antall || !allergiSelv || !folge || !allergiFolge) { setTimeout(setupForm, 250); return; } /* Ikke sett opp samme skjema flere ganger */ if (kommer.dataset.aalhyttaReady === '1') { return; } kommer.dataset.aalhyttaReady = '1'; /* ------------------------- Tekster ------------------------- */ if ( kommer.options && kommer.options.length > 0 && kommer.options[0].textContent !== '– Kommer du/dere? –' ) { kommer.options[0].textContent = '– Kommer du/dere? –'; } /* Fjern "(optional)" */ document.querySelectorAll('option').forEach(function(option) { option.textContent = option.textContent.replace(/\s*\(optional\)/gi, ''); }); document.querySelectorAll( 'input[placeholder], textarea[placeholder]' ).forEach(function(field) { field.placeholder = field.placeholder.replace(/\s*\(optional\)/gi, ''); }); /* ------------------------- Carrd-radene ------------------------- */ const antallRad = antall.parentElement; const allergiSelvRad = allergiSelv.parentElement; const folgeRad = folge.parentElement; const allergiFolgeRad = allergiFolge.parentElement; /* Antall personer skal aldri vises */ antallRad.style.setProperty( 'display', 'none', 'important' ); /* ------------------------- Farge på select ------------------------- */ function updateSelectColor() { if (kommer.selectedIndex === 0) { kommer.style.setProperty( 'color', 'rgba(255,255,255,0.55)', 'important' ); } else { kommer.style.setProperty( 'color', '#ffffff', 'important' ); } } /* ------------------------- Vis/skjul felter ------------------------- */ function updateForm() { const valg = kommer.selectedIndex; /* Ja, jeg kommer */ if (valg === 1) { antall.value = '1'; allergiSelvRad.style.removeProperty('display'); folge.value = ''; folgeRad.style.setProperty( 'display', 'none', 'important' ); allergiFolge.value = ''; allergiFolgeRad.style.setProperty( 'display', 'none', 'important' ); } /* Ja, jeg kommer med følge */ else if (valg === 2) { antall.value = '2'; allergiSelvRad.style.removeProperty('display'); folgeRad.style.removeProperty('display'); allergiFolgeRad.style.removeProperty('display'); } /* Nei */ else if (valg === 3) { antall.value = '0'; allergiSelv.value = ''; folge.value = ''; allergiFolge.value = ''; allergiSelvRad.style.setProperty( 'display', 'none', 'important' ); folgeRad.style.setProperty( 'display', 'none', 'important' ); allergiFolgeRad.style.setProperty( 'display', 'none', 'important' ); } /* Ingen valg */ else { antall.value = ''; allergiSelv.value = ''; folge.value = ''; allergiFolge.value = ''; allergiSelvRad.style.setProperty( 'display', 'none', 'important' ); folgeRad.style.setProperty( 'display', 'none', 'important' ); allergiFolgeRad.style.setProperty( 'display', 'none', 'important' ); } updateSelectColor(); } /* Bare én listener */ kommer.addEventListener('change', updateForm); updateForm(); } /* Start når siden er klar */ if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', setupForm); } else { setupForm(); } })();