Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Event handlers in files other than index.js

ANSWERED
import document from "document";

var myRect = document.getElementById("myRect");

myRect.onclick = function(e) {
  console.log("click");
}

 The above code works in index.js. What import or other statement do I need to have in index.js for it work if I want to move the code to another file?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

If you want to put that in a separate file, then you need to import that other file back into index.js.

 

 

// index.js
import * as blah from "./blah";
blah.initialize();

// blah.js
import document from "document";

export function initialize() {
var myRect = document.getElementById("myRect");

myRect.onclick = function(e) {
console.log("click");
}
}

 

View best answer in original post

Best Answer
1 REPLY 1

If you want to put that in a separate file, then you need to import that other file back into index.js.

 

 

// index.js
import * as blah from "./blah";
blah.initialize();

// blah.js
import document from "document";

export function initialize() {
var myRect = document.getElementById("myRect");

myRect.onclick = function(e) {
console.log("click");
}
}

 

Best Answer