Inserting DOM events

Hi there,

I have read on the Contact Form 7 website that the ‘on_sent_ok’ hook is soon to be ‘abolished’.

I am currently using it like this (in the additional settings tab of the form settings) to redirect to a thank you page for form submissions.

on_sent_ok: "location='/newsletter-thanks';"

The owner of Contact Form 7 now suggests utilizing Contact Form 7’s custom DOM event to run JavaScript. He says using this will achieve the same thing:

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://example.com/';
}, false );
</script>

When I insert the above code in the global javascript (in the theme’s customiser) it then stopped the other code (that was already in there) from working.

The code that I already have in there is this (which is for a convert plug pop up):

jQuery ( function( $ ) {

$('.pop-up-notice').on('click', function() {

$('.cp-image-close .cp-default-close, .cp-image-close').trigger('click');

} );

} );

How can I insert the DOM event code without affecting the existing code?

Also, I would like the DOM event code to only affect certain contact forms. I read that you can specify the ID of the contact form so that it only has an affect on certain forms.

They suggest inserting this:
if ( '123' == event.detail.contactFormId ) {

Problem is I don’t know where to insert it. Could you tell me if it is ok to insert it in this manner, so that the code then reads:

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = '/newsletter-thanks';
  if ( '123' == event.detail.contactFormId ) {
}, false );
</script>

Many thanks
Lewis

Hi Lewis,

You can add this javascript in Theme Options > JS.
Add it in the bottom most part so it wont affect any other javascript codes.

document.addEventListener( 'wpcf7mailsent', function( event ) {
 if ( '123' == event.detail.contactFormId ) {
    location = '/newsletter-thanks';
  }
}, false );

And note that you need to change 123 to the contact form id of the form you want to update.

Hope this helps.

Thanks, that worked!

I’m glad we could help

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.