Customer Portal

Get Involved. Join the Conversation.

Topic

    JJ
    Trigger a JS function after a form is submitted
    Topic posted September 7, 2018 by JJ Bronze Crown: 15,000+ Points 
    29 Views, 2 Comments
    Title:
    Trigger a JS function after a form is submitted
    Content:

    Hi,

    Using the code below - how would I then trigger an event to trigger after a successful submission?

    No custom widget being used - do I have to use one to achieve what I want?

    Thanks,

    JJ

    Code Snippet:

    Comment

     

    • eric montijo

      You could use JS to add an onsubmit or onclick event listener to the form or submit button. Here's some JS I've written from a project that might help. This is probably not plug-and-play code, but it should be a good start:

       

      <script>
      function y(){
      // grab the element needed
      var getcheckbox = document.getElementsByName("Contact.CustomFields.c.reg_from_community");
      // If you're using getElementsByName, remember it returns an array of all matching elements, so you may want to test to
      // see which element in the list it is. If you have a unique ID, you can more easily use getElementByID.
      var checkbox = getcheckbox[0];
      // these if statements determine if the browser should use addEventListener (most browsers) or attachEvent (predominantly IE)
      // in place of "myFunction" put the name of the function you want to fire on submit. You can use submit, click, pretty much 
      // any action that you can fire JS on in the HTML tag
      if (checkbox.addEventListener) {
      checkbox.addEventListener("click", myFunction);
      } else if (checkbox.attachEvent) {
      checkbox.attachEvent("click", myFunction);
      }
      // make sure to fire it, you can call the function earlier on during pageload, place it after the form, depends on your scope
      y();
      </script>
    • JJ

      Thanks - we do have JS like this already, I was just hoping there was a more native approach we could have took!