I have the following javascript class in foo.js
:
export default class Foo {
constructor(html) {
this.html = html;
}
}
which I'm importing and using as follows within select.js
:
import Foo from './foo.js'
var html = ``;
let array = ['bar', 'baz', 'boom']; // etc. etc.
for (var i = 0; i < array.length; i++) {
let element = array[i];
html += `<a onclick=`select('${element}');`>element</a>`;
}
let foo = new Foo(html);
$(`select`).html(foo.html);
function select(name) {
let foo = new Foo('foo'); // still module access ...
console.log(`foo.html = ${foo.html}`);
console.log(`name = ${name}`);
}
The select
element is defined in index.html
as:
<div id="select"></div>
<script type="module" src="./select.js"></script>
where I have imported the select.js
file as a module
.
However, I am getting the following error when I run this:
(index):1 Uncaught ReferenceError: select is not defined at HTMLAnchorElement.onclick ((index):1)
I need to have access to the class Foo
so this javascript must be a module. Is there anyway to achieve having the scope of select
within the index.html
file?
Edit
A solution proposed in the comments could be to assign a unique handler to each id
of the link elements after they have been embedded into the html, but this seems like a slightly verbose solution.