-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (47 loc) · 1.95 KB
/
script.js
File metadata and controls
57 lines (47 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// script.js
(() => {
const ratingCard = document.querySelector('.card.card--rating, .rating-state');
const thankyouCard = document.querySelector('.card.card--thankyou, .thankyou-state');
const form = document.querySelector('form.rating, .rating-form');
if (!form || !ratingCard || !thankyouCard) return; // por si el HTML cambia
const radios = form.querySelectorAll('input[name="rating"]');
const submitBtn = form.querySelector('.btn--submit, button[type="submit"]');
const selectedSlot = document.getElementById('selected-rating');
// Asegura que el grupo sea "requerido" (mejor en HTML, pero por si se olvida)
if (radios.length) radios[0].setAttribute('required', '');
// Deshabilita "Submit" hasta elegir una opción (opcional pero UX-friendly)
if (submitBtn) submitBtn.disabled = true;
form.addEventListener('change', (e) => {
if (e.target.name === 'rating' && submitBtn) {
submitBtn.disabled = false;
}
});
form.addEventListener('submit', (e) => {
e.preventDefault();
const checked = form.querySelector('input[name="rating"]:checked');
if (!checked) {
form.reportValidity(); // dispara la validación nativa
return;
}
// Inyecta el valor en la vista de "Thank you"
if (selectedSlot) selectedSlot.textContent = checked.value;
// Alterna vistas
ratingCard.hidden = true;
thankyouCard.hidden = false;
// Accesibilidad: mueve el foco al título de "Thank you"
const thankTitle = thankyouCard.querySelector('.card__title, h2, h1');
if (thankTitle) {
thankTitle.setAttribute('tabindex', '-1');
thankTitle.focus();
}
});
// Extra: permitir seleccionar con teclas 1–5
window.addEventListener('keydown', (e) => {
if (!/^[1-5]$/.test(e.key)) return;
const target = form.querySelector(`input[name="rating"][value="${e.key}"]`);
if (target) {
target.checked = true;
target.dispatchEvent(new Event('change', { bubbles: true }));
}
});
})();