Javascript clash

I am pretty new to javascript.

Working on a wordpress I typed the following function in the custom js for my theme:

Read More
document.getElementsByName("empty_cart")[0].onclick = function(e) {
  var choice = confirm("Are You Sure ?");
  if (!choice) {
    //Cancel submit
    e.preventDefault();
  }
};

This script allows for an “Are you sure you want to empty the cart?” confirmation, and works fine.

but when I added the following script that I copy and pasted from my email campaign provider ( robly ) the “are you sure” confirmation stopped working.

  $(document).ready(function () {

      $("#robly_embedded_subscribe").click(function (e) {
          e.preventDefault();

          var email = $("#DATA0").val();
          if (!is_valid_email_address(email)) {
              alert("Please enter a valid email address.");
              return false;
          }


          var f = $("#robly_embedded_subscribe_form");
          f.submit();

          return false;
      });

  });

  function is_valid_email_address(emailAddress) {
      var pattern = new RegExp(/^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$/i);
      return pattern.test(emailAddress);
  }

I have attempted to remove the function is_valid snippet and move that to my functions.php file in my child theme… but that didn’t work.

If anyone could help me out I would really appreciate it.

If you need any further information from me, please ask.

Related posts

Leave a Reply

1 comment

  1. Often times with WordPress you need to change all instances of $ to jQuery

    For example:

    $(document).ready(function () { ... });

    would become:

    jQuery(document).ready(function () { ... });