diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 75363cd7..bbf0f498 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,6 +17,7 @@ //= require jquery3 //= require popper //= require bootstrap-sprockets +//= require toggle_college_other_field $(document).on('turbolinks:load', function() { $('[data-toggle="tooltip"]').tooltip(); @@ -47,33 +48,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 = ''; + } + } + } +})