diff --git a/app/helpers/question_attribute_helper.rb b/app/helpers/question_attribute_helper.rb index d8ae9df3e..04f0ad4bc 100644 --- a/app/helpers/question_attribute_helper.rb +++ b/app/helpers/question_attribute_helper.rb @@ -21,9 +21,8 @@ private } end - # TODO: remove this once all conditional questions no longer need JS def conditional_html_attributes(question) - return {} if question.conditional_for.blank? || question.type == "radio" + return {} if question.conditional_for.blank? { "data-controller": "conditional-question", diff --git a/app/views/form/_radio_question.html.erb b/app/views/form/_radio_question.html.erb index d1fae6416..ffd18a536 100644 --- a/app/views/form/_radio_question.html.erb +++ b/app/views/form/_radio_question.html.erb @@ -14,7 +14,13 @@ <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) %> <% else %> <%= f.govuk_radio_button question.id, val, label: { text: val }, **stimulus_html_attributes(question) do %> - <%= render partial: "#{conditional_question.type}_question", locals: { question: conditional_question, caption: caption, page_header: page_header, f: f } %> + <%= render partial: "#{conditional_question.type}_question", locals: { + question: conditional_question, + caption: caption, + page_header: page_header, + f: f, + conditional: true + } %> <% end %> <% end %> <% end %> diff --git a/app/webpacker/controllers/conditional_question_controller.js b/app/webpacker/controllers/conditional_question_controller.js index dcb5c05d7..138efd2e9 100644 --- a/app/webpacker/controllers/conditional_question_controller.js +++ b/app/webpacker/controllers/conditional_question_controller.js @@ -20,21 +20,11 @@ export default class extends Controller { if(this.element.checked) { let selectedValue = this.element.value let conditional_for = JSON.parse(this.element.dataset.info) - Object.entries(conditional_for).map(([targetQuestion, conditions]) => { - let div = document.getElementById(targetQuestion + "_div") - if (div == null) return; + let input = document.getElementById(`case-log-${targetQuestion.replaceAll("_","-")}-field`) if(conditions.includes(selectedValue)) { - div.style.display = "block" } else { - div.style.display = "none" - let buttons = document.getElementsByName(`case_log[${targetQuestion}]`); - if (buttons.length == 0){ - buttons = document.getElementsByName(`case_log[${targetQuestion}][]`); - } - Object.entries(buttons).map(([idx, button]) => { - button.checked = false; - }) + input.value = "" } }) }