Task: There are three steps in the form, after selecting one of the elements, the selected step, and the next one appears. By clicking on an item you need to send its id to the registration object, how to do it?
const request = new XMLHttpRequest(); var steps = document.querySelectorAll('.master-change, .service-change, .date-block'); // Π‘ΠΊΡΡΠ²Π°Π΅ΠΌ Π²ΡΠ΅ ΡΠ°Π³ΠΈ ΠΏΡΠΈ Π·Π°Π³ΡΡΠ·ΠΊΠ΅ ΡΡΡΠ°Π½ΠΈΡΡ window.onload = function() { steps.forEach((element, index) => { if (index > 0) { element.classList.add('hide'); } }); }; //ΠΠ΅ΡΠ΅Ρ
ΠΎΠ΄ ΠΏΠΎ ΡΠ°Π³Π°ΠΌ var choiseElement = document.querySelectorAll('.master-list, .service-list'); // ΠΡΠ±ΠΈΡΠ°Π΅ΠΌ ΡΠ»Π΅ΠΌΠ΅Π½ΡΡ Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΎΡΠ»Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΠΊΠ»ΠΈΠΊ choiseElement.forEach((element, index) => { element.addEventListener('click', function() { nextStep(element, index); // ΠΠ΅ΡΠ°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΠ΅ Π½Π° ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· Π½ΠΈΡ
}, false); }); var registration = { masterName: '', serviceType: '', time: '' }; function nextStep(el, index) { steps[index].classList.toggle('hide'); // ΠΠΎΡΠ»Π΅ ΠΊΠ»ΠΈΠΊΠ° ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈΡΡΠ΅Π·Π°Π΅Ρ steps[index + 1].classList.toggle('hide'); // Π ΠΈΠ΄ΡΡΠΈΠΉ Π·Π° Π½ΠΈΠΌ ΠΏΠΎΡΠ²Π»ΡΠ΅ΡΡΡ } const submit = document.querySelector('.btn-submit'); submit.addEventListener('click', function() { event.preventDefault(); // ΠΡΠΌΠ΅Π½ΡΠ΅ΠΌ Π΄Π΅ΠΉΡΡΠ²ΠΈΠ΅ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ request.open('GET', './scripts/info.json', true); request.setRequestHeader('Content-type', './scripts/info.json; charset=UTF-8'); request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { // alert(request.responseText); } else { console.log(request.status + ' ' + request.statusText); } } request.send(); console.log(registration); }); .hide { display: none; } <main class="pageBody"> <div class="index"> <div class="container"> <div class="form-block"> <form class="form-order" name="form-order"> <div class="from-order__step from-order__step-1 master-change"> <div class="master-list"> <div class="master-list__item" id="master_1"><img class="master-list__photo" src="http://via.placeholder.com/64x64" width="64px" height="64px" alt="Master photo"> <div class="master-name" id="master_1" href="#">Master name</div> </div> <div class="master-list__item" id="master_2"><img class="master-list__photo" src="http://via.placeholder.com/64x64" width="64px" height="64px" alt="Master photo"> <div class="master-name" id="master_2" href="#">Master name</div> </div> <div class="master-list__item" id="master_3"><img class="master-list__photo" src="http://via.placeholder.com/64x64" width="64x" height="64px" alt="Master photo"> <div class="master-name" id="master_3" href="#">Master name</div> </div> </div> </div> <div class="from-order__step from-order__step-2 service-change hide"> <div class="service-list"> <div class="service-list__item" id="service_1"> <h3 class="service__item__name">Service name </h3> <p class="service__item__description">Service description Service description Service description Service description</p><span class="service__price">999 rub</span><span class="service__time">14:44</span> </div> <div class="service-list__item" id="service_2"> <h3 class="service__item__name">Service name </h3> <p class="service__item__description">Service description Service description Service description Service description</p><span class="service__price">999 rub</span><span class="service__time">14:44</span> </div> <div class="service-list__item" id="service_3"> <h3 class="service__item__name">Service name </h3> <p class="service__item__description">Service description Service description Service description Service description</p><span class="service__price">999 rub</span><span class="service__time">14:44</span> </div> </div> </div> <div class="from-order__step from-order__step-3 date-block hide"> <div class="date-block__calendar"> <div class="date-block__time"> <select class="date-block__time"> <option>10:00</option> <option>11:00</option> <option>12:00</option> <option>13:00</option> <option>14:00</option> <option>15:00</option> <option>16:00</option> <option>17:00</option> <option>18:00</option> <option>19:00</option> <option>20:00</option> </select> </div> <button class="btn btn-submit" type="submit">ΠΡΠΏΡΠ°Π²ΠΈΡΡ</button> </div> </div> </form> </div> </div> </div> </main>
ΠΎΡΠΏΡΠ°Π²ΠΈΡΡ Π΅Π³ΠΎ id Π² ΠΎΠ±ΡΠ΅ΠΊΡ registrationwhat does it mean? just take the ID of the element and stuff it into the object? - Rostyslav Kuzmovychelement.id- Rostyslav Kuzmovych