Task Post Button + addEventListener: Unable to abort posting of comments

michaelsweetser
Kilo Explorer

I'm attempting to throw a warning message to the user whenever additional comments are submitted, confirming if the user means to post customer-visible data.   This works if saving/updating the record via an onSubmit client script.   Since the "post" button isn't affected by this, I'm attempting to use addEventListener on an onLoad script to accomplish the same thing.

function onLoad() {

      var workNotesDiv = document.querySelector(".comment-box");

      var buttons = workNotesDiv.querySelectorAll("button");

      var postBtn;

      for (var i = 0; i < buttons.length; i++) {

              if (buttons[i].firstChild.nodeValue.includes("Post"))

                      postBtn = buttons[i];

      }

      if (postBtn) {

              postBtn.addEventListener("click", checkCommentsAlert);

      }

}

function checkCommentsAlert() {

      if (!g_form.hasField('comments'))

              return;

      if (g_form.getValue('comments') != '') {

            if (confirm('You are about to submit comments to this record that will be visible by customers.'))

                  return true;

            else {

                  g_form.submitted = false;

                  g_form.getControl('comments').focus();

                  return false;

            }

      }

}

Everything works except that it doesn't abort the submission; even though it returns false, the value can't go anywhere and thus, the form submission goes through unaffected.

How can I fix this issue?   Thanks in advance!

1 REPLY 1

Community Alums
Not applicable

Function inside addEventListener() doesn't return anything, so this way you probably won't be able to prevent posting.

Anyway, was anyone able to find a solution for this? It would help me a lot as well. 🙂