$(document).ready(function () { // var highest_amount = 0; $('input[name^=\'amount_\']').change( function () { var clickedRadio = this; var afterClickedRadio = false; var formIsValid = true; var radios = document.querySelectorAll('input[name^=\'amount_\']'); // Automatic completion for (i = 0; i < radios.length; ++i) { var radio = radios[i]; if (radio == clickedRadio) { afterClickedRadio = true; continue; } if (!afterClickedRadio && clickedRadio.value == 'left' && radio.value == 'left') { radio.checked = true; } if (afterClickedRadio && clickedRadio.value == 'right' && radio.value == 'right') { radio.checked = true; } } // Check if list complete for (i = 0; i < radios.length; ++i) { var radio = radios[i]; if (!radio.checked) { if (radio.value == "right") { if (!radios[i-1].checked) { formIsValid = false } } if (radio.value == "left") { if (!radios[i+1].checked) { formIsValid = false } } } } if (formIsValid) { // Set switching point variable for (i = 0; i < radios.length; ++i) { var radio = radios[i]; // if (radio.dataset.amount > highest_amount) { // highest_amount = radio.dataset.amount // } if (radio.value == 'right' && radio.checked) { $('#switching_point').val(radio.dataset.amount); break; } else { $('#switching_point').val(9999); } }; // Enable button if all radio buttons checked $('#submit_posterior').prop('disabled', false); } } ); })