CMS Development

PeterKumospace
Member

Module HTML to JS Function Calls Not Working

SOLVE

I have a module I'm building and as part of it I need to have some buttons call some javascript. The problem I'm facing is that when they buttons try to make the JS calls I get errors saying that the function is not defined. It looks like the JS minification changing the function names doesn't update the function names in the HTML calls so it breaks things.

I could put the javascript into script tags in the HTML to avoid the problem I'm facing, but that would cause the script to be loaded for each instance of the module rather than only once per page. Is there another way I should be creating these functions/making these calls so that the buttons can call the js functions?

0 Upvotes
1 Accepted solution
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Module HTML to JS Function Calls Not Working

SOLVE

Instead of calling the function from within your HTML, call it from within an event listener in your Javascript. Something like:

 

document.querySelector('.your-button-class').addEventListener('click', function() {
  // your function code here
});

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.

View solution in original post

1 Reply 1
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Module HTML to JS Function Calls Not Working

SOLVE

Instead of calling the function from within your HTML, call it from within an event listener in your Javascript. Something like:

 

document.querySelector('.your-button-class').addEventListener('click', function() {
  // your function code here
});

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.