Reputation: 1
I'm trying to capture the wpcf7submit event from a Contact Form 7 form on my WordPress site to trigger an alert when the form is successfully submitted. However, the event doesn't seem to trigger the function I've attached to it. Here's the code snippet I'm using within a script tag in my footer.php file, after the body content:
<div class="contact-form">
<h2 class="contact-form__title">Contact</h2>
<?php echo apply_shortcodes('[contact-form-7 id="fbe8376" title="Contact Form 1"]'); ?>
</div>
document.addEventListener('DOMContentLoaded', function() {
const wpcf7Elm = document.querySelector('.wpcf7');
console.log(wpcf7Elm); // For debugging: to verify that the element is being selected.
wpcf7Elm.addEventListener('wpcf7submit', function(event) {
alert("Fire!");
}, false);
});
I've confirmed that the .wpcf7 element exists in the DOM by checking the console output, which correctly logs the element. This indicates that the script correctly waits for the DOM to be fully loaded before attempting to attach the event listener.
Despite this, the alert doesn't appear when the form is submitted. I've checked for JavaScript errors in the console but haven't found any that seem related. The form itself works fine and submits as expected.
I'm wondering if there are any known issues with capturing wpcf7submit events like this, or if there's something wrong with how I'm trying to attach the event listener. Could there be a conflict with another script, or does Contact Form 7 require a different approach to capture submission events?
Upvotes: 0
Views: 406