I'm doing an app for a class, and the professor is just as confused as I am about why it's not working. I have 2 js files and one html. I'm exporting an array from one js to the other, then using that js as the src in my html. Here's the pertinent code:
medList.js:
export const CompleteMedList = [
.....
];
main.js:
import {CompleteMedList} from './MedList.js'
html:
<script src="main.js"></script>
And here's the error I'm given:
main.js:1 Uncaught SyntaxError: Unexpected token {
I tried using type="module" in the script tag, but then it gives me this error when I try do do stuff using the functions declared in main.js:
Uncaught ReferenceError: openCycleForm is not defined at HTMLButtonElement.onclick
Here's the function that the error references in main.js:
function openCycleForm() {
document.getElementById("newCyclePg1").style.width = "100%";
for (var i=0; i < CompleteMedList.length; i++){
document.getElementById("fullMedsList").innerHTML +=
"<input type=\"checkbox\" name=\"meds\" id=\"" + CompleteMedList[i].id + "\">" +
CompleteMedList[i].name + "<br></br>";
}
}
I initially had the code in the html file (where it worked), but just moved it to main.js so I could use an array of data instead of hardcoding it, and then everything broke...
mainthen, astype="module"solved the other error. If it's not much code add it here, or add it on jsFiddle so we can have a quick look. - Andy