0

Is it possible to call a function inside a module from html?

For example, like something below:

es6 module: main.js

function download(){
}
export {download}

html file:

<script type="module" src="main.js"></script>
<button id="download" onclick="download()"></button>
danilo
  • 7,680
  • 7
  • 43
  • 46
  • Does this answer your question? [Use functions defined in ES6 module directly in html](https://stackoverflow.com/questions/53630310/use-functions-defined-in-es6-module-directly-in-html) and [ES6 Modules: Undefined onclick function after import](https://stackoverflow.com/questions/44590393/es6-modules-undefined-onclick-function-after-import) – Bergi May 25 '22 at 00:31
  • (Of course, the [proper solution is to use `document.getElementById('download').onclick = download;` instead of an inline event handler attribute](https://stackoverflow.com/q/6941483/1048572)) – Bergi May 25 '22 at 00:32

1 Answers1

1

I found the solution.
The Html searchs for functions residing in window object.
So it is necessary to assign the function to window object:

es6 module: main.js

function download(){
}
window.download = download
danilo
  • 7,680
  • 7
  • 43
  • 46