Я рекомендую привязать одну и ту же функцию-обработчик ко всем входам.
Это помогает избежать повторения (см. DRY) и потенциально способствует более удобному, гибкому и легкому коду.
Вот пример:
- Привязать обработчик событий
change
ко всем входам. - Когда вход изменяется, повторите все шаги и найдите их проверенные входы.
- Для каждого шага добавьте его проверенное значение (или неопределенное) в массив.
const $steps = $('.stepArea');
const $inputs = $('.inptAnswer');
const $output = $('#output');
let stepValues = [];
function getStepValues() {
stepValues = $.map($steps, function(step) {
return $('.inptAnswer:checked', step).val() || [undefined];
});
$output.text(stepValues);
}
$inputs.on('change', getStepValues);
#output {
margin: 1em 0;
padding: 1em;
background: lightblue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="stepShow">
<div class="stepArea stepAreaActive">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
</div>
<div id="output"></div>
Без jQuery, только для ударов:
const steps = document.querySelectorAll('.stepArea');
const inputs = document.querySelectorAll('.inptAnswer');
const output = document.getElementById('output');
let stepValues = [];
function getStepValues() {
stepValues = Array.from(steps).map((step) => {
let input = step.querySelector('.inptAnswer:checked');
return input ? input.value : undefined;
});
output.innerHTML = stepValues;
}
inputs.forEach((input) => {
input.addEventListener('change', getStepValues);
});
#output {
margin: 1em 0;
padding: 1em;
background: lightblue;
}
<div class="stepShow">
<div class="stepArea stepAreaActive">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
<div class="stepArea">
<form class="form">
Choose whether this video is fake or real?
<input class="inptAnswer" type="radio" name="choice" value="real"> Real
<input class="inptAnswer" type="radio" name="choice" value="fake"> Fake
</form>
</div>
</div>
<div id="output"></div>