HTMLSnippet HTML and JS combined

0
Dear Community,   I'm working on creating an HTML form where, based on the selected radio buttons, a JavaScript function writes a cookie. The HTML form itself is complete and functions well. However, I'm encountering an issue with combining two content types (HTML and JavaScript) in a single file. I've attempted to use two separate HTML snippets—one with a content type of HTML and another with a content type of JavaScript—but this approach hasn't been successful. How can I effectively integrate both HTML and JavaScript in this scenario? Thank you for your assistance!  
asked
2 answers
0

It may be easier to use a JavaScript Action as you can call these from Nanoflows. They allow you work with custom JavaScript directly in your app.

 

For example, if someone selects a radio button, you can trigger an on-change event to call a nanoflow, which in turn can call the JavaScript action. You can then set the cookie in the JavaScript action.

 

https://docs.mendix.com/howto/extensibility/build-javascript-actions/

 

I hope this helps, good luck

answered
1

I finally got it to work!

 

The solution involved two parts: an HTML snippet (Content-Type: HTML) and a JavaScript snippet (Content-Type: JS). Here’s what I did:

 

  1. I added a JavaScript file in my /theme/ folder.
  2. In the HTML snippet, I used the 'External file' field to refer to this JavaScript file.

As a result, the HTML snippet remains essentially empty, with the content type set to JS, and it references the JS file in the /theme/ folder.

 

That did the trick. Thanks for your help, everyone!

answered