user7331530
user7331530

Reputation: 985

How to access same element in multiple javascript files

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?

Upvotes: 0

Views: 36

Answers (1)

Max
Max

Reputation: 799

you need just export CONFIRM_BUTTON without curved brackets

Upvotes: 1

Related Questions