I have some HTML with a button whose onclick
event triggers a JavaScript function.
I need to achieve the same behavior when the JavaScript function is an ES6 module function.
I have a simple example:
<head>
<meta charset="utf-8">
<script src="./js1.js"></script>
<script type="module" src="./js2.js"></script>
<script type="module">
import { Es6Module3 } from "./js3.js";
document.querySelector('#myFunction3').addEventListener('click', Es6Module3.myFunction3);
</script>
</head>
<body>
<button onclick="myFunction1(1)">button1</button>
<button onclick="Es6Module2.myFunction2(2)">button2</button>
<button type="button" id="myFunction3">button3</button>
</body>
It's also reproduced at https://jsfiddle.net/avnerm/jho6qgLu/18/
When clicking button1, the function myFunction1 in js1.js is called, and the value that is passed in is printed properly into the console. good!
But when clicking button2 I get a message:
ReferenceError: Es6Module is not defined
At some point I was able to trigger a module function (the use case of button3) by setting an event listener, but it's not working now and I don't know how to pass a parameter into the function in this implementation.
In my case, I need to be able to pass in a parameter.
How can I call a ES6 module function from the onclick attribute, and pass a parameter to it?
EDIT:
@CertainPerformance, My original html code involves jinja2, which spans the code 3 times (3 groups with ids: 1, 2, 3)
I used your pattern, and it created 3 buttons (button1, button2, button3) with 3 event listeners. When I click on each button I can see in the log the appropriate group id.
This works for me perfectly!
I just wanted to confirm that this is the way to deal with html, jinja2, and es6.
Thanks!
class Es6Module2 {
static myFunction2 = function(val) {
console.log('BEG myFunction2');
console.log('val');
console.log(val);
};
};
export { Es6Module2 };
{% block content %}
<div class="admin-view-groups">
{% for group in groups %}
<button id="button{{group.id}}">Download Group</button>
<script type="module">
import { Es6Module2 } from './Es6Module2.js';
document.querySelector('#button{{group.id}}').addEventListener('click', () => {
Es6Module2.myFunction2({{ group.id }});
});
</script>
{% endfor %}
</div>
{% endblock %}