From 7cba160564448a520c14a098ce4dd30c4bc2f06c Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Thu, 19 Mar 2020 21:08:44 -0400 Subject: cleaned up js --- src/js/index.js | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) (limited to 'src/js') diff --git a/src/js/index.js b/src/js/index.js index 21adad5..0697b51 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,12 +1,9 @@ - /* * Adds an event listener to all burgers to toggle navbar menu on click. */ document.addEventListener('DOMContentLoaded', function () { - // get all navbar-burger elements const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); - // check if there are any navbar burgers if ($navbarBurgers.length > 0) { // add a click event on each of them $navbarBurgers.forEach(function(el) { @@ -23,11 +20,34 @@ document.addEventListener('DOMContentLoaded', function () { } }); -document.forms.inquiry.onsubmit = function() { +const disableErrorBox = function() { + document.getElementById("errorBox").style.display = "none"; +} + +const disableSuccessBox = function() { + document.getElementById("successBox").style.display = "none"; +} + +const enableErrorBox = function(message) { let errorBox = document.getElementById("errorBox"); - let successBox = document.getElementById("successBox"); - errorBox.style.display = "none"; - successBox.style.display = "none"; + let aTag = document.createElement('a'); + aTag.setAttribute("href", "mailto:amy@carpentertutoring.com"); + aTag.innerText = message + " Click here to send through your mail client."; + while (errorBox.hasChildNodes()) { errorBox.removeChild(errorBox.lastChild); } + errorBox.appendChild(aTag); + errorBox.style.display = "block"; +} + +const enableSuccessBox = function() { + document.getElementById("successBox").style.display = "block"; +} + +/* + * Adds mail sender to form submit. + */ +document.forms.inquiry.onsubmit = function() { + disableErrorBox(); + disableSuccessBox(); let form = document.forms.inquiry; let name = form.elements.name.value; let fromEmail = form.elements.from.value; @@ -40,20 +60,12 @@ document.forms.inquiry.onsubmit = function() { Subject : "Inquiry: " + name, Body : body, }).then( message => { - if (message != "OK") { - let link = " Click here to send through your mail client." - let aTag = document.createElement('a'); - aTag.setAttribute("href", "mailto:amy@carpentertutoring.com"); - aTag.innerText = message + " Click here to send through your mail client."; - while (errorBox.hasChildNodes()) { - errorBox.removeChild(errorBox.lastChild); - } - errorBox.appendChild(aTag); - errorBox.style.display = "block"; + if (message == "OK") { + form.reset(); + enableSuccessBox(); } else { - form.reset(); - successBox.style.display = "block"; + enableErrorBox(message); } } ); -- cgit v1.2.3