0

Caldera forms plugin for wordpress returns a URL similar to the one below when an error occurs during submission of a form.

?cf_er=_cf_process_5e7a1d0c43fbe

How, using jQuery, could I add a class to the body if the url contains AT LEAST this part;

?cf_er

Many thanks in advance for any help offered.

2 Answers2

1

use includes to check existence of string ?cf_er.

const url = "?cf_er=_cf_process_5e7a1d0c43fbe";

if (url.includes('?cf_er')) {
  $(document.body).addClass('someclass');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
random
  • 7,756
  • 3
  • 19
  • 25
  • Browser support for `.includes()` [here](https://caniuse.com/#feat=es6-string-includes) Just a note for anyone unfortunate enough to be supporting IE11 and below. – DBS Mar 24 '20 at 16:35
  • I dont understand why I provide a sample url which clearly includes the relevant section.... and then I check the url to see if it contains that section..... Can you help me to understand? – Briany Hearne Mar 24 '20 at 17:34
0

Assuming you get the URL as a string returned from the "Caldera forms plugin":

   const url = caldera_forms_plugin();

   if (url.includes('?cf_er') {
      $('body').addClass(MY_CLASS_NAME);
   }

But you don't really need to use jQuery to add a class to body, just do:

    body.classList.add(MY_CLASS_NAME);
terrymorse
  • 6,771
  • 1
  • 21
  • 27