Help with - Cannot set property 'onclick' of null

I am creating a pop-up when a button on a site is clicked and everything works fine until it's time to close the pop-up. I have created a close button with JS but now when trying to apply it, I get `Cannot set property 'onclick' of null`. Any idea's why my `#btn2` is considered a null value? Here's the code:

var vaderPopup = function() {

	//Create elements
	var cover = document.createElement('img');
	var closeBtn = document.createElement('div');

	//Assign id's to elements
	cover.id = "cover";
	closeBtn.id = "closeBtn";

	//Add image src to popup
	cover.src = "images/click-vader.jpg";
	cover.style.display = 'block';
	cover.style.margin = 'auto';
	cover.style.zIndex = 100;
	cover.style.width = '100%';
	cover.style.height = '100%';
	cover.style.position = 'absolute';
	cover.style.top = '180px';
	cover.style.left = 0;
	cover.style.opacity = '.9';

	//Add CSS to closeBtn
	closeBtn.style.backgroundColor = '#A7A7AC';
	closeBtn.style.border = '1px solid #CCC';
	closeBtn.style.textAlign = 'center';
	closeBtn.style.position = 'absolute';
	closeBtn.style.top = '100%';
	closeBtn.style.right = '50%';
	closeBtn.style.bottom = '50%';
	closeBtn.style.left = '43%';
	closeBtn.style.width = '200px';
	closeBtn.style.height = '100px';
	closeBtn.style.padding = '0 8px 0 8px';
	closeBtn.style.opacity = '.8';
	closeBtn.style.zIndex = '200';

	//Add content to closeBtn
	closeBtn.innerHTML = "<h3><strong>You have chosen wisely...</strong></h3>";
	closeBtn.innerHTML += "<button id='btn2'>Return</button>";

	//Add cover and closeBtn to page
	document.querySelector('#vader').onclick = function() {
		document.querySelector('body').appendChild(cover);
		document.querySelector('body').appendChild(closeBtn);
	}

	var closePopup = function() {
		cover.style.display = 'none';
		closeBtn.style.display = 'none';
	}

	document.querySelector('#btn2').onclick = closePopup;

}();

Open in new window

LVL 2
Courtney WhitingConversion Lead / Information ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
document.querySelector('#btn2').onclick = closePopup;

Open in new window

Click event handlers are registered with the page on load as they are encountered and you cannot register a click handler to a non-existant object.
"btn2" is a non-existent object because it is not appended to the body until "vader" is clicked.
document.querySelector('#vader').onclick = function() {
		document.querySelector('body').appendChild(cover);
		document.querySelector('body').appendChild(closeBtn);
	}

Open in new window

Try adding the click event handler for "btn2" AFTER "vader" is clicked.
document.querySelector('#vader').onclick = function() {
		document.querySelector('body').appendChild(cover);
		document.querySelector('body').appendChild(closeBtn);
		document.querySelector('#btn2').onclick = closePopup;
	}

Open in new window

Another way to handle this would be to append the "cover" and "closeBtn" to the page on load but set display:none for those elements. Then upon "vader" click, set to display:block. On "closeBtn" click set back to display:none.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Courtney WhitingConversion Lead / Information ArchitectAuthor Commented:
Nice! I used your suggested of 'Try adding the click event handler for "btn2" AFTER "vader" is clicked.' because it worked the best for my purposes. I also added:
	var reOpen = function() {
		cover.style.display =  'block';
		closeBtn.style.display = 'block';
	}

Open in new window

and:

	document.querySelector('#vader').onclick = function() {
		document.querySelector('body').appendChild(cover);
		document.querySelector('body').appendChild(closeBtn);
		document.querySelector('#btn2').onclick = closePopup;
		document.querySelector('#vader').onclick = reOpen;
	}

Open in new window


to re-add the pop-up, kinda what you were suggesting in your second idea.

Thanks!
Tom BeckCommented:
You're welcome. Thanks for the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.