From 2c74f41f04474c9e749d62f395cc498cc622ab65 Mon Sep 17 00:00:00 2001 From: Janell-Huyck Date: Thu, 16 Nov 2023 15:13:10 -0500 Subject: [PATCH] Move code to toggle college 'other' field to own file --- app/assets/javascripts/application.js | 31 +------------------ .../javascripts/toggle_college_other_field.js | 29 +++++++++++++++++ 2 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 app/assets/javascripts/toggle_college_other_field.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index f3577e48..b64a0475 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -18,6 +18,7 @@ //= require popper //= require bootstrap-sprockets //= require bootstrap-tooltip-initialization +//= require toggle_college_other_field function addAuthor(type, count) { var count = count || 1; @@ -44,33 +45,3 @@ function addAuthor(type, count) { function removeAuthor(idToDelete) { document.getElementById(idToDelete).outerHTML = ''; } - -$(document).on('turbolinks:load', function() { - if (window.location.pathname.includes("/edit") || window.location.pathname.includes("/new")) { - otherChkbox = document.querySelector('[id$="_college_ids_16"]'); - otherField = document.querySelector('[id$="_other_college"]'); - otherGroup = document.querySelector('[id$="other_college_group"]'); - tempStorage = ''; - - if (otherField.value != "") { - otherChkbox.checked = true; - } - else { - otherGroup.classList.toggle("d-none"); - } - - otherChkbox.onclick = function() { - if (otherChkbox.checked) { - otherGroup.classList.toggle("d-none"); - if (tempStorage != '') { - otherField.value = tempStorage; - } - } - else { - otherGroup.classList.toggle("d-none"); - tempStorage = otherField.value; - otherField.value = ''; - } - } - } -}) \ No newline at end of file diff --git a/app/assets/javascripts/toggle_college_other_field.js b/app/assets/javascripts/toggle_college_other_field.js new file mode 100644 index 00000000..81f2f3b2 --- /dev/null +++ b/app/assets/javascripts/toggle_college_other_field.js @@ -0,0 +1,29 @@ +$(document).on('turbolinks:load', function() { + if (window.location.pathname.includes("/edit") || window.location.pathname.includes("/new")) { + otherChkbox = document.querySelector('[id$="_college_ids_16"]'); + otherField = document.querySelector('[id$="_other_college"]'); + otherGroup = document.querySelector('[id$="other_college_group"]'); + tempStorage = ''; + + if (otherField.value != "") { + otherChkbox.checked = true; + } + else { + otherGroup.classList.toggle("d-none"); + } + + otherChkbox.onclick = function() { + if (otherChkbox.checked) { + otherGroup.classList.toggle("d-none"); + if (tempStorage != '') { + otherField.value = tempStorage; + } + } + else { + otherGroup.classList.toggle("d-none"); + tempStorage = otherField.value; + otherField.value = ''; + } + } + } +})