I tried this:
in constant.js
const CONFIRM_BUTTON = document.querySelector(".confirm-button");
export { CONFIRM_BUTTON };
in some-file.js,
import { CONFIRM_BUTTON } from './constants';
CONFIRM_BUTTON.onclick = someFunction;
I am getting Cannot set property 'onclick' of null error in doing so.
If in some-file.js file, I do following, it works:
const CONFIRM_BUTTON = document.querySelector(".confirm-button");
CONFIRM_BUTTON.onclick = someFunction;
I am trying to do this const CONFIRM_BUTTON = document.querySelector(".confirm-button"); once and use it multiple js files? How can I do that?
document.querySelector(...)is evaluated, it will returnnull..confirm-buttonin not in the DOM on initial load, but instead of importing CONFIRM_BUTTON element, if I declare it in the same file and use it, it works. This works ` const CONFIRM_BUTTON = document.querySelector(".confirm-button");