0

I'm using Vite in a nodejs environment to build a ThreeJS app. The scene and all the ThreeJS elements, functions are stored in main.js .

I want to execute a function when I click a button, that's why I use the "onclick" attribute. I don't want to overcharge the app by using an event listener to that button.

My problem is that the console is saying the said function is undefined, and after some tests all the functions from that file are undefined. The browser console can't execute them, and none of the nodes present on the page can execute the functions.

Since the code relies on imports, the file is linked to the html file with this tag type="module" meaning the whole page is a module. I tried to use the "export" keyword when declaring the function but I can't use imports in a onclick property so it didn't work.

I'm sure the function works since when I call it from the module (in the same file it got declared) I can see it is executed in the browser. I don't know what to do, I'm pretty sure there's a solution but even by looking in Mozilla's documentation I couldn't find anything... Have a nice day and thank you for reading... Feel free to ask for more context if you need so :)

  • Thanks, can you provide some example code to show us how you're attaching the function to the onclick method? Some things to consider: 1. Be sure the functions are being loaded before the HTML is generated, otherwise, it may be undefined at the time of creation. 2. The "onclick" attribute is an Event Attribute, which is also an event handler. You could try programmatically setting this handler to help test out the feature. – Tom King Apr 29 '23 at 16:40
  • Yeah no problem :)https://ibb.co/WVJ7g2v https://ibb.co/FqWPGk6 it's a free image hoster – Sundae Not Apr 29 '23 at 17:13
  • "*I don't want to overcharge the app by using an event listener to that button.*" - what? – Bergi Apr 29 '23 at 18:49
  • 1
    @SundaeNot Please [do not post a link to a painting of the code](https://meta.stackoverflow.com/q/285551/1048572)! Instead, [edit] your question to include it as [formatted text](/help/formatting). – Bergi Apr 29 '23 at 18:50

0 Answers0