Title: JavaScript events &#8211; error: undefined
Last modified: March 25, 2020

---

# JavaScript events – error: undefined

 *  Resolved [kubajjz](https://wordpress.org/support/users/kubajjz/)
 * (@kubajjz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/)
 * Hi, if I putt this code (copied from your Knowledge Base) to my website:
 *     ```
       <script>
       document.querySelector('.hf-form-356').on('hf-success', function(e) {
          // your code goes here
       });
       </script>
       ```
   
 * I get an error:
    `'document.querySelector('.hf-form-356').on' is undefined)`
 * Do you know, where is a problem?
    Thanks a lot, Jakub
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fjavascript-events-error-undefined%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Lap](https://wordpress.org/support/users/lapzor/)
 * (@lapzor)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12582211)
 * Where on that page can I find the HTML form?
 * Thanks for letting me know.
 *  Thread Starter [kubajjz](https://wordpress.org/support/users/kubajjz/)
 * (@kubajjz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12582223)
 * The red button in header.
 * But you can easily see the error in conslole > navigate to error.
    Or search (
   ctrl/cmd+f) in code…
 *  [Lap](https://wordpress.org/support/users/lapzor/)
 * (@lapzor)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12582920)
 * Maybe HTML forms script isn’t loaded yet at the time you execute that JS code.
 * Can you add
 *     ```
       document.addEventListener("DOMContentLoaded", function(event) { 
         //code goes here
       });
       ```
   
 * and see if that solves the issue for you?
 * Hope that helps. If you have any questions, please let me know!
 *  Thread Starter [kubajjz](https://wordpress.org/support/users/kubajjz/)
 * (@kubajjz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12582981)
 * I have tried many conditions (your solution as well) and I am loading it after
   everything was fully loaded. And I still get same error:
 *     ```
       TypeError: document.querySelector('.hf-form-356').on is not a function. (In 'document.querySelector('.hf-form-356').on('hf-submit', function(e) {
           console.log('sent');
           window.dataLayer.push({
            'event': 'FormSent'
           });
         })', 'document.querySelector('.hf-form-356').on' is undefined)
       ```
   
 * I have run out of solutions… 🙁
 *  Thread Starter [kubajjz](https://wordpress.org/support/users/kubajjz/)
 * (@kubajjz)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12583230)
 * I finally found something. It is working in jquery not in javascript…
    When I
   wrap it in jquery, it is working. I think, it is because “document.querySelector”
   will select specific form, but you can work with it as an object.
 * Here is working code – in default WP jQuery v1.12.4 with anonym wrapper function
 *     ```
       <script>
       (function ($) {
           $(window).ready(function () {
               $('.hf-form-356').on('hf-submit', function(e) {
                   console.log('email-send');
                   window.dataLayer.push({
                       'event': 'FormSent'
                   });
               });
           });
       }(jQuery));
       </script>
       ```
   
 * Have a nice day,
    Jakub
 *  [Lap](https://wordpress.org/support/users/lapzor/)
 * (@lapzor)
 * [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12583995)
 * Awesome, Thanks for letting me know.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘JavaScript events – error: undefined’ is closed to new replies.

 * ![](https://ps.w.org/html-forms/assets/icon-256x256.png?rev=1754345)
 * [HTML Forms - Simple WordPress Forms Plugin](https://wordpress.org/plugins/html-forms/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/html-forms/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/html-forms/)
 * [Active Topics](https://wordpress.org/support/plugin/html-forms/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/html-forms/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/html-forms/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Lap](https://wordpress.org/support/users/lapzor/)
 * Last activity: [6 years, 2 months ago](https://wordpress.org/support/topic/javascript-events-error-undefined/#post-12583995)
 * Status: resolved